View Full Version : Creating links dynamically
habib
09-23-2002, 11:21 AM
Hi,
I want to be able to change a <a href> link dynamically depending on the cookie value of a user.
For example:
if a user's cookie value is UK, I want to change <a href = "blah.htm"> to <a href = "uk.htm">
if a user's cookie value is US, I want to change <a href = "blah.htm"> to <a href = "us.htm">...
and so on.
Is this possible?
Or is there a simpler way of achieving this
Thanx
HR
:)
Alekz
09-23-2002, 11:31 AM
Hi,
Try:
<a href="javascript:doLink(cookieValue)">Link</a>
Then
function doLink(cookieVal){
switch (cookieVal){
case "UK":
document.location = "UK.htm";
break;
case "US":
document.location = "US.htm";
break;
}
}
Alex
habib
09-23-2002, 11:51 AM
Hi Alex,
I tried the following and I got an 'expected label for break error' I'm not sure what this means as I haven't used the 'switch' and 'break' functions before.
<script>
function doLink(cookieVal){
switch (cookieVal){
case "UK":
document.location = "contact.htm";
break;
case "US":
document.location = "location-africa-southafrica.htm";
break;
}
}
</script>
<a href="javascript:doLink(cookieValue)">Contact Us</a>
Habib
Alekz
09-23-2002, 11:59 AM
Hi,
I've never heard about such an error.... which browser are You using?
Anyway, You probably could get around this using
if(cookieVal == "UK")
document.location = "UK.html";
else if(cookieVal == "US")
document.location = "US.html";
and so on...
Nothe that in
<a href="javascript:doLink(cookieValue)">Contact Us</a>
cookieValue have to be replaced with a real string value either client or server side...
Alex
habib
09-23-2002, 12:03 PM
I am using IE5.5 and DreamWeaver as my web package. When I use the de-bug feature in DreamWaver it gave me this error message. When I previewed the page in my Web browser and clicked on Contact Us I got an 'error on page'
Alekz
09-23-2002, 12:08 PM
Yes, I suppose You will... Try this:
<a href="javascript:alert(cookieValue);doLink(cookieValue)">Contact Us</a>
And tell me what's alerted....
As I said cookieValue should be a valid string - it have no value in the example I posted, You'll have to assign it...
Alex
habib
09-23-2002, 01:06 PM
Hi
The error i get is:
function getCookie(name){
var cname = name + "=";
var dc = document.cookie;
if (dc.length > 0) {
begin = dc.indexOf(cname);
if (begin != -1) {
begin += cname.length;
end = dc.indexOf(";", begin);
if (end == -1) end = dc.length;
return unescape(dc.substring(begin, end));
}
}
return null;
}
This is the function i used to get the cookie value and then based on this the link would change.
Alekz
09-23-2002, 01:37 PM
What if You write it like this:
<script>
function doLink(){
var name = 'some hardcoded cookie name';
var cookieVal = getCookie(name);
switch (cookieVal){
case "UK":
document.location = "contact.htm";
break;
case "US":
document.location = "location-africa-southafrica.htm";
break;
}
}
</script>
<a href="javascript:doLink()">Contact Us</a>
Alex
habib
09-23-2002, 02:30 PM
This is what I have got on my script. Function getCookie is stored in an external.js file which is referenced in the <head> Now I get the error home_location is undefined. I'm well confused now
<script>
function doLink(){
var name = 'home_location';
var cookieVal = getCookie(home_location);
switch (cookieVal){
case "UK":
document.location = "contact.htm";
break;
case "US":
document.location = "location-africa-southafrica.htm";
break;
}
}
</script>
<a href="javascript:doLink()">Contact Us</a>
Alekz
09-23-2002, 02:53 PM
<script>
function doLink(){
var name = 'home_location';
var cookieVal = getCookie(name);
switch (cookieVal){
case "UK":
document.location = "contact.htm";
break;
case "US":
document.location = "location-africa-southafrica.htm";
break;
}
}
</script>
<a href="javascript:doLink()">Contact Us</a>
Or directly:
var cookieVal = getCookie('home_location');
Alex
habib
09-23-2002, 03:39 PM
Now I get the following error:
Object doesn't support this property or method
line 128
in the script line 128 is as follows:
document.location = "contact.htm";
does this mean that this syntax is not supported?
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.