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.
– cpcolavite
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).
– Sorack