Results 1 to 7 of 7

Thread: Cookie help for URL

  1. #1
    Join Date
    Mar 2005
    Location
    Western Australia
    Posts
    148
    Thanks
    24
    Thanked 4 Times in 4 Posts

    Default Cookie help for URL

    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

  2. #2
    Join Date
    Mar 2007
    Location
    Currently: New York/Philadelphia
    Posts
    2,735
    Thanks
    3
    Thanked 519 Times in 507 Posts

    Default

    I hope I make sense
    No, can't say that it does actually.

    This article 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.

  3. #3
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    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:

    Code:
    <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):

    Code:
    <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>
    Last edited by jscheuer1; 08-08-2008 at 03:42 AM. Reason: fix typo
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  4. #4
    Join Date
    Mar 2005
    Location
    Western Australia
    Posts
    148
    Thanks
    24
    Thanked 4 Times in 4 Posts

    Default

    OK thanks guys I am learning

    so far I have

    Code:
    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

    Code:
    <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

    Code:
    <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

  5. #5
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    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.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  6. #6
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    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):

    Code:
    <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>
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  7. The Following User Says Thank You to jscheuer1 For This Useful Post:

    gwmbox (08-08-2008)

  8. #7
    Join Date
    Mar 2005
    Location
    Western Australia
    Posts
    148
    Thanks
    24
    Thanked 4 Times in 4 Posts

    Default

    Worked a treat - very much appreciated

    Thanks

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •