Create URL with cookies from a different page and redirect with JS

I am newbie in JS but I managed to create the code thanks to the feedback from users of this site. Explain the code first:

The objective is to create a URL link basing on two pages (A and B).

The page A loads a cookie using a hidden form. This page also contains a link that redirects to page B:

// CODE FROM SOURCE PAGE A //
<a href="to_page_b">TO PAGE B</a>

<form name="sender">
<input type="hidden" name="message" size="30" value="customvalue"/>
</form>
<script type="text/javascript">
function setCookie(value) {
    document.cookie = "cookie-msg-test=" + value + "; path=/";
    return true;
}
function updateMessage() {
    var t = document.forms['sender'].elements['message'];
    setCookie(t.value);
    setTimeout(updateMessage, 100);
}
updateMessage();
</script>

Page B has a list of links with different values assigned. With JS call the cookie from page A and assign it to a variable. When we click on a link assign the value to another variable. JS creates the new URL with these two variables and redirects.

// CODE FOR DESTINATION PAGE B //
<a id="LNK" href="#" value="value_for_url" onclick="clickLink(this)">CUSTOM REDIRECTION</a>
<a id="LNK" href="#" value="other_value_for_other_url" onclick="clickLink(this)">OTHER CUSTOM REDIRECTION</a>
// RECEIVE THE COOKIE FROM SOURCE PAGE A //
<script type="text/javascript">
function getCookie() {
    var cname = "cookie-msg-test=";
    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(cname) == 0) {
            return c.substring(cname.length, c.length);
        }
    }
    return null;
}
// PASTE THE COOKIE AND THE VALUE OF PAGE A AND REDIRECT //
function clickLink(a) {
    var url_part_from_a = a.getAttribute('value');
    var url_part_from_cookie = getCookie();
    window.location.assign("http://domain/"+url_part_from_a+url_part_from_cookie)
       return false;
    }
</script>

As will be different pages A with different valors of cookies I would modify the page A code so that when you click on the link assign the value to the cookie and not before.

Any ideas? Thanks in advance

Upvotes: 0

Views: 16457

Answers (1)

Solved!

CODE FOR PAGE A:

<html>
<body>
<a href="http://page_B.html" value="custom_code_a" onclick="clickLink(this)">LINK TO PAGE B</a>
<script type="text/javascript">
    function clickLink(a) {
        var url1 = a.getAttribute('value');
        document.cookie = 'cookiename=' +url1+'; expires=Wed, 1 Jan 2070 13:47:11 UTC; path=/';
    }
</script>
</body>
</html>

CODE FOR PAGE B

<html>
<body>
<a id="LNK" href="#" value="custom_code_b" onclick="clickLink(this)">JS REDIRECT</a>    
<script type="text/javascript">
    function getCookie(cookiename)
      {
        var re = new RegExp(rgcookie + "=([^;]+)");
        var value = re.exec(document.cookie);
        return (value != null) ? unescape(value[1]) : null;
      }
    var url1 = getCookie("cookiename");
    function clickLink(a) {
    var url2 = a.getAttribute('value');
    window.location.assign("http://domain/"+url1+url2);
    }
</script>
</body>
</html>

Upvotes: 1

Related Questions