Simplified Javascript Cookie Value Capture

Asked

Viewed 97 times

4

To accomplish my work I need to capture in a simplified way the values present in the First Cookie and Second Cookie. The value of each of these cookies is a variable web path (in Primerocaminho is where the main window points; in Second Path is where an accessory window, a <iframe>, points). The intention is to store information from where the user has been and, when he returns to http://www.meusite.com it will be returned exactly on the pages where it stopped, both main and accessory.

To create the cookie I am using:

<script language="javascript">
    document.cookie = 'PrimeiroCaminho=' + window.location + '; expires=Mon, 21 Nov 2078 12:00:00 UTC; path=/';
</script>

For cookie reading this was my first attempt...

<script language="javascript">
    var re = new RegExp(PrimeiroCaminho + "=");
    var value = re.exec(document.cookie);
    return (value != null) ? decodeURI(value[1]) : null);
    var str = getCookie("PrimeiroCaminho");
    if (str.match(/html/)) {
        document.write ("<meta http-equiv='refresh' content='3;' + getCookie('PrimeiroCaminho') + ' />");
        }
    else if {
        document.write ("<meta http-equiv='refresh' content='4;http://www.meusite.com/' />");
        }
</script>

...and this is my second, which I believe is closer to the right...

<script language="javascript">
    var user = getCookie("PrimeiroCaminho");
    var name = PrimeiroCaminho + "=";
    var decodedCookie = decodeURIComponent(document.cookie);
    var ca = decodedCookie.split(';');
    for(var i = 0; i < ca.length; i++) {
        var c = ca[i];
        while (c.charAt(0) == ' ') {
            c = c.substring(1);
            }
        if (c.indexOf(name) == 0) {
            document.write ("<meta http-equiv='refresh' content='3;' + return c.substring(name.length, c.length) + ' />");
            }
        }
        else if {
            document.write ("<meta http-equiv='refresh' content='4;http://www.meusite.com/' />");
            }
</script>

...but I haven’t had any success yet. The intention, relative to the main window, is for the command to respect this dynamic that if the First Cookie exists, redirect (through <meta> timed in 3s) the user for the value stored in the First Cookie, but, if this First Cookie did not exist, redirect (through <meta> timed in 4s) the user to http://www.meusite.com/.

Regarding the accessory window the schema would be the same. Everything in a very simple way. Thank those who can help this curious poor in javascript.


I’ve been digging a lot into the code and found this solution, which was not quite what I wanted (no timer; the method forces me to keep folders and subfolders with the same number of characters so that the slice always work at the right point; the method forces me to hide in the cookie some código to be able to be checked) but it worked for what I wanted:

<script language="javascript">
    var cookiecontent = document.cookie.slice(9, 81);
    var str = cookiecontent;
    if (str.match(/código/)) {
        location.href = PrimeiroCaminho;
        }
    else if (str.length == 0) {
        location.href = "http://www.meusite.com/";
        }
</script>

If someone eventually thinks of a solution, be sure to post!

  • I put the alternative solution at the end of the question.

  • Has any response helped solve the problem and can address similar questions from other users? If so, make sure to mark the answer as accepted. To do this just click on the left side of it (below the indicator of up and down votes).

1 answer

0

By adapting the code of the solution next to what you want, you can use the split to catch the last occurrence of / and the timer can be added with setTimeout:

setTimeout(function() {
  var partes = document.cookie.split('/');
  var cookiecontent = partes(partes.length - 2);
  var str = cookiecontent;

  if (str === 'código')) {
    location.href = encodeURIComponent(PrimeiroCaminho);
  } else if (str.length == 0) {
    location.href = "http://www.meusite.com/";
  }
}, 4000);
  • It worked perfectly! But both in my last alternative solution, as in yours, the location.href in Internet Explorer, Edge and Chrome just doesn’t work. In Firefox and Opera it’s working properly. Something between them works differently?

  • About the timer I had conditioned my alternative solution to a function VerifyCookie() and in the body, put something like that: onload="setTimeout(function(){VerifyCookie()},5000);. This worked on Firefox and Opera, but as I said, evolving the script routine to location.href, in Internet Explorer, Edge and Chrome simply doesn’t work. Very strange! By the way, your solution is much more elegant, so I adopted it!

  • Perhaps an alternative to location.href, for what I’ve been researching, it could be to do the document.write write down <meta http-equiv='refresh' content='5;url=PrimeiroCaminho' />. But this didn’t work in any browser.

  • @cpcolavite do not forget to give an upvote and accept the answer if it met you

  • An addendum on the location.href: running on Firefox (desktop and mobile), Opera (desktop and mobile), Edge (mobile) and Chrome (mobile); not running on Internet Explorer (desktop), Edge (desktop) or Chrome (desktop).

Browser other questions tagged

You are not signed in. Login or sign up in order to post.