Jump to content

Recommended Posts

Posted

 

So I found a cookie script that's pretty simple which saves a user's preference for a page. Here's the JS code:

>function getCookie(NameOfCookie){
   if (document.cookie.length > 0) {              
   begin = document.cookie.indexOf(NameOfCookie+"=");       
   if (begin != -1) {           
     begin += NameOfCookie.length+1;       
     end = document.cookie.indexOf(";", begin);
     if (end == -1) end = document.cookie.length;
       return unescape(document.cookie.substring(begin, end));
   } 
 }
 return null;
}

function setCookie(NameOfCookie, value, expiredays) {
var ExpireDate = new Date();
ExpireDate.setTime(ExpireDate.getTime() + (expiredays * 24 * 3600 * 1000));
document.cookie = NameOfCookie + "=" + escape(value) + 
((expiredays == null) ? "" : "; expires=" + ExpireDate.toGMTString());
}

function delCookie (NameOfCookie) {
if (getCookie(NameOfCookie)) {
 document.cookie = NameOfCookie + "=" +
 "; expires=Thu, 01-Jan-03 00:00:01 GMT";
 }
}

function DoTheCookie() {
Preference=getCookie('Preference');
if (Preference!=null) {
 window.location.href="http://exampleaddress.com/"+Preference;
 }
else {
 window.location.href="http://exampleaddresss.com/index.html";
}
}

 

in the HTML page the body tags is like this:

><body onload="preloadImages(); DoTheCookie();">

the preloadImages is another function somewhere else...

 

Then the html link for one page is like so:

><a href="#" onClick="setCookie('Preference','index2.html',365)">flash version</a>

 

so, the cookie does save the users' preference but it keeps reloading the page incessantly instead of only once! I think its the body onLoad function, but I don't know enough!

 

feel free to use the code, I found it online; it just didn't come with a lot of explanation. story of my life. :D

 

thanks a mil,

!!blue

 

Posted

I looked at the code.

 

And your right, there's nothing thre to STOP the browser from reloading the URL. It just adds on any preferences it happens to find and still reloads the page.

 

You need something that checks the end of the URL, or looks for the parameters passed, before executing the reload.

 

Does that make sense?

Posted (edited)

 

that makes sense, but I wouldn't know how to do it :dance:

 

It's like my Spanish, I can read it but I can't write it. :dance:

 

I'll try to search for another cookie; what if I changed the html page by removing function from the body "onLoad" and did the html link like this:

><a href="index2.html" onClick="setCookie('Preference','index2.html',365)">flash version</a>

so the pages link AND the preference is set. but I guess that wouldn't work when the user returns on another visit (?).

 

meh,

!!blue

Edited by !!blue

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...