Forum Moderators: open

Message Too Old, No Replies

Adding captured url around script

Cookie url in javascript

         

multichild

2:45 pm on Jan 8, 2009 (gmt 0)

10+ Year Member



I have a cookie script that works very well, but its missing wrapping the captured url string around the hotel name I captured in the cookie (line 3).

function readCookie(name) {
var nameEQ = name + "=";
[b]var url = location.href;[/b]
var ca = document.cookie.split(';');
for(var i=0;i < ca.length;i++) {
var c = ca[i];
while (c.charAt(0)==' ') c = c.substring(1,c.length);
if (c.indexOf(nameEQ) == 0) {
hotel = c.substring(nameEQ.length,c.length);
//alert (hotel);
[b]document.getElementById("messageBlock").innerHTML = "Welcome to " + hotel;[/b]
return hotel;
}

I want the link with the captured url to start after 'Welcome to' and before 'hotel' on line 11.

Cheers

Lee

multichild

4:04 pm on Jan 8, 2009 (gmt 0)

10+ Year Member



I've tried this, but it doesnt work:


document.getElementById("messageBlock").innerHTML = ("<a href="var url">")"Welcome to " + hotel("</a>");

I'm probably way off the mark mind

eelixduppy

4:10 pm on Jan 8, 2009 (gmt 0)



How 'bout this:

document.getElementById("messageBlock").innerHTML = "<a href=\"" + url + "\">Welcome to " + hotel + "</a>";

And Welcome to WebmasterWorld!

multichild

4:15 pm on Jan 8, 2009 (gmt 0)

10+ Year Member



Oh fantastic, now thats what I'm talking about. One person, one answer. Thank you very much, appreciate that.

And thanks for the welcome.

Cheers

Lee

multichild

11:50 am on Jan 15, 2009 (gmt 0)

10+ Year Member



Hi again,

I got that last bit working a treat, but I now have another problem which I have tried and tried to get working and I just cant work it out.

As you will see, ive used PHP too from what I found on the web, but if that php works to grab the url its just not either passing it over in the cookie, or the cookie reader isnt able to read it.

Here is the cookie creater code on one page:


<body onload="createCookie('hotelcookie', '<?=$rows['Nom_Hot']?>', 365, '<? $url = $_SERVER['SERVER_NAME']; $page = $_SERVER['php_SELF']; echo "http://".$url.$page; ?>')">

<script type = "text/javascript">
function createCookie(name,value,days,url) {
if (days) {
var date = new Date();
date.setTime(date.getTime()+(days*24*60*60*1000));
var expires = "; expires="+date.toGMTString();
}
else var expires = "";
document.cookie = name+"="+value+expires+url+"; path=/";
}

Here is the cookie reader code:


<body onload="readCookie('hotelcookie')">

<script type = "text/javascript">

function readCookie(name) {
var nameEQ = name + "=";
var urlRead = url;
var ca = document.cookie.split(';');
for(var i=0;i < ca.length;i++) {
var c = ca[i];
while (c.charAt(0)==' ') c = c.substring(1,c.length);
if (c.indexOf(nameEQ) == 0) {
hotel = c.substring(nameEQ.length,c.length);
//alert (hotel);
document.getElementById("messageBlock").innerHTML = "On your last visit you viewed "+"<a href=\"" + urlRead + "\">" + hotel +"</a>";
return hotel;
}
}
return null; // no display if no previous visit
}

function eraseCookie(name) {
createCookie(name,"",-1);
}
</script>

Everything else works fine, I can grab the hotel name and pass it over and it displays, but when it comes to passing over the url too and putting it to the hotel name its not workio.

Can anybody help please.

Cheers

Lee

multichild

2:30 pm on Jan 15, 2009 (gmt 0)

10+ Year Member



I can get the hotel name to appear by adding url into that first line

function readCookie(name,url) {

But then the hotel name appears and the url the hotel is pointing too is not it, just the home page url with undefined as the later part of the url.

The only thing that missing is the correct url being passed over to go with the hotel name thats displayed. I cant be either picking it up correct on the cookie display page, or its not pushing the url into the cookie, or more likely knowing me both!

I am trying honest.

Lee