www.webdeveloper.com
Results 1 to 5 of 5

Thread: Javascript SetCookie and URL Redirect

  1. #1
    Join Date
    Aug 2009
    Posts
    4

    Javascript SetCookie and URL Redirect

    I need some help writing a script to set a cookie and then read the cookie and place in a URL redirect.

    Here is the scenario:

    We have a site corp site that offers a free website to consultants. We are able to add content to this corp website (html or wysiwyg).

    We also have a custom site we have built. I need a link that will set a cookie on click - similar to this (but not sure if the syntax is correct for this to work):

    <a href="(url)" onclick="setCookie('(cookie_name)','(cookie_value)',5);go_to('(url)');">Link Name</a>

    When this link is clicked - it is pushed over to the corp site.

    On the corp site - I need a script that will call to see if the cookie above has been set, and if the cookie exists - redirects to the URL specified in the value of the cookie.

    <script>
    IF getcookie('cookie_name') EXISTS
    THEN
    window.location = '(value of cookie)';
    </script>

    The reason we are doing this is because we need to be able to link to specific products on the corp site provided to us, but the product links will not work correctly without the corp site setting a session variable they set, based on the url they provide, identifying the consultant before the product page can be viewed.

    So the above will allow: to set a cookie with a URL for a value - send the link to the main corp url provided so it can set its cookies to recognize the consultant - then the script recognizes the cookie we set to redirect to the product page we wish to link to.

    I hope this is clear enough as I am very ignorant when it comes to javascript.

    Thanks in advance for the help.

    Chuck

  2. #2
    Join Date
    Aug 2009
    Posts
    37
    Quote Originally Posted by rpmind View Post
    I need some help writing a script to set a cookie and then read the cookie and place in a URL redirect.

    Here is the scenario:

    We have a site corp site that offers a free website to consultants. We are able to add content to this corp website (html or wysiwyg).

    We also have a custom site we have built. I need a link that will set a cookie on click - similar to this (but not sure if the syntax is correct for this to work):

    <a href="(url)" onclick="setCookie('(cookie_name)','(cookie_value)',5);go_to('(url)');">Link Name</a>

    When this link is clicked - it is pushed over to the corp site.

    On the corp site - I need a script that will call to see if the cookie above has been set, and if the cookie exists - redirects to the URL specified in the value of the cookie.

    <script>
    IF getcookie('cookie_name') EXISTS
    THEN
    window.location = '(value of cookie)';
    </script>

    The reason we are doing this is because we need to be able to link to specific products on the corp site provided to us, but the product links will not work correctly without the corp site setting a session variable they set, based on the url they provide, identifying the consultant before the product page can be viewed.

    So the above will allow: to set a cookie with a URL for a value - send the link to the main corp url provided so it can set its cookies to recognize the consultant - then the script recognizes the cookie we set to redirect to the product page we wish to link to.

    I hope this is clear enough as I am very ignorant when it comes to javascript.

    Thanks in advance for the help.

    Chuck

    you can try this -

    <html>
    <head><title>send cookie</title>

    <script type='text/javascript'>

    var expdate = new Date();expdate.setTime(expdate.getTime() + (24 * 60 * 60 * 1000 * 365));

    function getCookieVal (offset) {
    var endstr = document.cookie.indexOf (";", offset); if (endstr == -1)
    endstr = document.cookie.length;
    return unescape(document.cookie.substring(offset, endstr)); }

    function GetCookie (name) { var arg = name + "="; var alen = arg.length;
    var clen = document.cookie.length; var i = 0; while (i < clen) {
    var j = i + alen; if (document.cookie.substring(i, j) == arg)
    return getCookieVal (j); i = document.cookie.indexOf(" ", i) + 1;
    if (i == 0) break; } return null; }

    function SetCookie (name, value) {
    var argv = SetCookie.arguments;
    var argc = SetCookie.arguments.length;
    var expires = (2 < argc) ? argv[2] : null;

    var path = (3 < argc) ? argv[3] : null;
    var domain = (4 < argc) ? argv[4] : null;
    var secure = (5 < argc) ? argv[5] : false;
    document.cookie = name + "=" + escape (value) +
    ((expires == null) ? "" : ("; expires=" + expires.toGMTString())) +
    ((path == null) ? "" : ("; path=" + path)) +
    ((domain == null) ? "" : ("; domain=" + domain)) +
    ((secure == true) ? "; secure" : ""); }


    function DeleteCookie (name,path,domain) {
    if (GetCookie(name)) {
    document.cookie = name + "=" +
    ((path) ? "; path=" + path : "") +
    ((domain) ? "; domain=" + domain : "") +
    "; expires=Thur, 01-Jan-70 00:00:01 GMT";
    }
    }

    function set_href_cookie(href_val){

    SetCookie('href_location', href_val, expdate, '/', null, false);
    document.location.href=href_val;
    }

    </script>

    </head>

    <body>

    <a href="javascript:void(0);" onclick="set_href_cookie('http://www.microsoft.com')">www.microsoft.com</a>

    <script type='text/javascript'>

    cookie_name = GetCookie("href_location");

    if(cookie_name){
    alert("Cookie found, redirecting to stored cookie.");
    document.location.href=cookie_name;
    }

    </script>

    </body>
    </html>

  3. #3
    Join Date
    Aug 2009
    Posts
    37
    <html>
    <head><title>send cookie</title>

    <script type='text/javascript'>

    var expdate = new Date();expdate.setTime(expdate.getTime() + (24 * 60 * 60 * 1000 * 365));

    function getCookieVal (offset) {
    var endstr = document.cookie.indexOf (";", offset); if (endstr == -1)
    endstr = document.cookie.length;
    return unescape(document.cookie.substring(offset, endstr)); }

    function GetCookie (name) { var arg = name + "="; var alen = arg.length;
    var clen = document.cookie.length; var i = 0; while (i < clen) {
    var j = i + alen; if (document.cookie.substring(i, j) == arg)
    return getCookieVal (j); i = document.cookie.indexOf(" ", i) + 1;
    if (i == 0) break; } return null; }

    function SetCookie (name, value) {
    var argv = SetCookie.arguments;
    var argc = SetCookie.arguments.length;
    var expires = (2 < argc) ? argv[2] : null;

    var path = (3 < argc) ? argv[3] : null;
    var domain = (4 < argc) ? argv[4] : null;
    var secure = (5 < argc) ? argv[5] : false;
    document.cookie = name + "=" + escape (value) +
    ((expires == null) ? "" : ("; expires=" + expires.toGMTString())) +
    ((path == null) ? "" : ("; path=" + path)) +
    ((domain == null) ? "" : ("; domain=" + domain)) +
    ((secure == true) ? "; secure" : ""); }


    function DeleteCookie (name,path,domain) {
    if (GetCookie(name)) {
    document.cookie = name + "=" +
    ((path) ? "; path=" + path : "") +
    ((domain) ? "; domain=" + domain : "") +
    "; expires=Thur, 01-Jan-70 00:00:01 GMT";
    }
    }

    function set_href_cookie(href_val){

    SetCookie('href_location', href_val, expdate, '/', null, false);
    document.location.href=href_val;
    }

    </script>

    </head>

    <body>


    <a href="javascript:void(0);" onclick="set_href_cookie('http://www.microsoft.com')">www.microsoft.com</a>

    <script type='text/javascript'>

    cookie_name = GetCookie("href_location");

    if(cookie_name){
    alert("Cookie found, redirecting to stored cookie.");
    document.location.href=cookie_name;
    }

    </script>

    </body>
    </html>

  4. #4
    Join Date
    Aug 2009
    Posts
    4
    OK...tried your suggestion to no avail.

    This is what I think is the problem: We are setting a cookie for the domain-A.com, and not the domain-B.com. The cookie sets, but when URL forwarded to domain-B.com from domain-A.com, it cant find that cookie, because it is attached to domain-A.com and not Domain-B.com.

    We need to be able to set a cookie for domain-B.com, so it can document.location.href to the value of the cookie.

    Or, can you tell java to read a cookie on Domain-B.com from Domain-A.com when calling the value? If you can do that, we can set the cookie on Domain-A.com, then the script we place on the Domain-B.com can read the value from Domain-A.com.

    This is the problem, and the script above sets the cookie for Domain-A.com, Forwards the browser to Domain-B.com and tries to fetch the cookie set, but cant find it, because its attached to Domain-A.com and not Domain-B.com.

    Instead of writing enormous collaborative code, can we not set the cookie using a simple embedded &lt;a&gt; link javascript?

    This is the link on Domain-A.com::::

    <a href="http://www.domain-A.com/" onclick="setCookie('url_path','https://www.domain-B.com//forms/frm_shop_item_info.aspx?prod=BAR',5);go_to('http://www.domain-B.com/consultant');">Buy Online</a>

    I need a simple script on Domain-B.com to capture the cookie set on Domain-A.com:::
    <script>
    If domain-A.com.cookie.url_path Is Defined
    Then document.location.href = domain-A.com.cookie.url_path;
    Else
    Do Nothing
    </script>

    Any suggestions?

    Thanks.

    Chuck

  5. #5
    Join Date
    Aug 2009
    Posts
    1

    Lightbulb

    Quote Originally Posted by rpmind View Post
    This is what I think is the problem: We are setting a cookie for the domain-A.com, and not the domain-B.com. The cookie sets, but when URL forwarded to domain-B.com from domain-A.com, it cant find that cookie, because it is attached to domain-A.com and not Domain-B.com.

    We need to be able to set a cookie for domain-B.com, so it can document.location.href to the value of the cookie.
    The browser will not send a cookie from one domain to another. Why use cookies at all? Have you tried adding another query string parameter with the value of the cookie you were going to set. Then just read that parameter on the destination page from javascript.

    -Ken

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles