Log in

View Full Version : Cookie help for URL



gwmbox
08-08-2008, 03:11 AM
Hi guys

I am still trying to find my answer by searching and reading the results (many of them) but if anyone has a quick bit of code that will help then please post away :) and help me out :)

I want to be able to set a cookie (or other alternative) to the URL of the page the user is currently on as they click on a link (and only if they click on that link) - and then on a page one, two ,three or even four clicks later they have another link which says return to X which is the URL that was set by the cookie. At that time (returning back to the page provided by the cookie, the cookie is reset again if they go to any other page which has a similar link and so on... I hope I make sense

Hope you can help

Cheers

Medyman
08-08-2008, 03:25 AM
I hope I make sense
No, can't say that it does actually.

This article (http://www.quirksmode.org/js/cookies.html) will explain how to set and read cookies. Perhaps you can try creating your own version from this and post back with any troubles?

The setting the initial URL, reading it and setting a link, then clearing it is fairly straightforward. You lost me with the clicks in the middle though. Maybe if you could provide some context, it would be clearer.

jscheuer1
08-08-2008, 03:36 AM
Cookies can be a little tricky. There are various sets of functions around that simplify their use, even so a fairly good understanding of javascript and the concept of cookies is still very helpful. I would recommend:

http://www.quirksmode.org/js/cookies.html

Not perfect, but very good.

Now, if you have server side code available on your host, that would be better to use.

Using javascript and the functions from the above link, when a user clicks on the link that's supposed to store the cookie, you could have:


<a href="whatever.htm" onclick="createCookie('storedURL',window.location.href + '::' + document.title);return true;">Whatever</a>

It will store the URL of the page and it's title, so make sure the page has a title. The title will be used for the text portion of the back link later.

Then on the page where you want to retrieve the value and provide a link back to it, you could have this script (place it on the page in the spot where you want the link to appear):


<script type="text/javascript">
;(function(){
if(readCookie('storedURL')){
var stored = readCookie('storedURL').split('::');
document.write('<a href="' + stored[0] + '">Back to ' + stored[1] + '<\/a>');
}})();
</script>

gwmbox
08-08-2008, 04:57 AM
OK thanks guys I am learning :)

so far I have


function createCookie( sName, sValue, iDays ){
sValue = escape( sValue );
if( iDays ){
var oDate = new Date();
oDate.setTime( oDate.getTime() + ( iDays*24*60*60*1000 ) );
var sExpires = "; expires="+oDate.toGMTString();
}
else
var sExpires = "";
document.cookie = sName+"="+sValue+sExpires+"; path=/";
}

function readCookie(sName) {
var sNameEQ = sName + "=";
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(sNameEQ) == 0) return c.substring(sNameEQ.length,c.length);
}
return null;
}

function eraseCookie(sName) {
createCookie(sName,"",-1);
}


and in my html file to createCookie


<a href="myfile.html" rel="nofollow" onclick="createCookie('storedURL',window.location.href + '::' + document.title,2);return true;">Whatever</a>

and in the HTML file to readCookie


<script type="text/javascript">
;(function(){
if(readCookie('storedURL')){
var stored = readCookie('storedURL').split('::');
document.write('<a href="' + stored[0] + '">Back to ' + stored[1] + '<\/a>');
}})();
</script>

And it appears to be working but when I select the Back to... link I get something like

http://mydomain.com/http://mydomain.com/....

Note the two http's and domains - how do I fix that?

Cheers

jscheuer1
08-08-2008, 05:07 AM
Well, it's (what I posted) working fine here:

http://home.comcast.net/~jscheuer1/side/back_cookie/

To determine the problem with your code (if any, it might be some other issue, perhaps with your server), either way it would help to have a link to the page:

Please post a link to the page on your site that contains the problematic code so we can check it out.

jscheuer1
08-08-2008, 06:11 AM
OK, I got your PM with the URL to the test site. I see the problem but cannot be certain of the best solution. The problem is that, probably due to the fact that the original URL contains a query string, the cookie is URLencoding the entire stored value. As a result, when we go to break it up into its component parts later, the separator I used has been encoded and therefore no longer available for use in delineating the stored string. This creates an invalid URL that looks to the browser like a relative URL, so it prepends the current location as a base href.

This should work, but may need tweaking or correcting, I don't see a way to test it fully here (it at least passes the idiot test - works for pages without the issue I just outlined):


<script type="text/javascript">
;(function(){
if(readCookie('storedURL')){
var stored = unescape(readCookie('storedURL')).split('::');
var URL = stored[0].split('?');
if(URL[1] && URL[1].length > 0)
URL[1] = '?' + escape(URL[1]);
else
URL[1] = '';
document.write('<a href="' + URL[0] + URL[1] + '">Back to ' + stored[1] + '<\/a>');
}})();
</script>

gwmbox
08-08-2008, 06:31 AM
Worked a treat - very much appreciated :)

Thanks