Js Cookie

liunx

Guest
<!--fonto:Verdana--><span style="font-family:Verdana"><!--/fonto--><!--sizeo:3--><span style="font-size:12pt;line-height:100%"><!--/sizeo--><!--coloro:#336699--><span style="color:#336699"><!--/coloro--><br /><br />So I found a cookie script that's pretty simple which saves a user's preference for a page. Here's the JS code:<br /><!--c1--><div class='codetop'>CODE</div><div class='codemain'><!--ec1-->function getCookie(NameOfCookie){<br />    if (document.cookie.length > 0) {              <br />    begin = document.cookie.indexOf(NameOfCookie+"=");       <br />    if (begin != -1) {           <br />      begin += NameOfCookie.length+1;       <br />      end = document.cookie.indexOf(";", begin);<br />      if (end == -1) end = document.cookie.length;<br />        return unescape(document.cookie.substring(begin, end));<br />    } <br />  }<br />  return null;<br />}<br /><br />function setCookie(NameOfCookie, value, expiredays) {<br />    var ExpireDate = new Date();<br />    ExpireDate.setTime(ExpireDate.getTime() + (expiredays * 24 * 3600 * 1000));<br />    document.cookie = NameOfCookie + "=" + escape(value) + <br />    ((expiredays == null) ? "" : "; expires=" + ExpireDate.toGMTString());<br />}<br /><br />function delCookie (NameOfCookie) {<br />    if (getCookie(NameOfCookie)) {<br />  document.cookie = NameOfCookie + "=" +<br />  "; expires=Thu, 01-Jan-03 00:00:01 GMT";<br />  }<br />}<br /><br />function DoTheCookie() {<br />    Preference=getCookie('Preference');<br />    if (Preference!=null) {<br />  window.location.href=http://www.totalchoicehosting.com/forums/lofiversion/index.php/"http://exampleaddress.com/"+Preference;<br />  }<br />    else {<br />  window.location.href=http://www.totalchoicehosting.com/forums/lofiversion/index.php/"http://exampleaddresss.com/index.html";<br />    }<br />}<!--c2--></div><!--ec2--><br /><br />in the HTML page the body tags is like this:<!--c1--><div class='codetop'>CODE</div><div class='codemain'><!--ec1--><body onload="preloadImages(); DoTheCookie();"><!--c2--></div><!--ec2--> the preloadImages is another function somewhere else...<br /><br />Then the html link for one page is like so:<br /><!--c1--><div class='codetop'>CODE</div><div class='codemain'><!--ec1--><a href=http://www.totalchoicehosting.com/forums/lofiversion/index.php/"#" onClick="setCookie('Preference','index2.html',365)">flash version</a><!--c2--></div><!--ec2--><br /><br />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!<br /><br />feel free to use the code, I found it online; it just didn't come with a lot of explanation. story of my life. <img src="http://www.totalchoicehosting.com/forums/style_emoticons/default/rolleyes.gif" style="vertical-align:middle" emoid=":rolleyes:" border="0" alt="rolleyes.gif" /> <br /><br />thanks a mil,<br />!!blue<br /><br /><!--sizec--></span><!--/sizec--><!--colorc--></span><!--/colorc--><!--fontc--></span><!--/fontc--><!--content-->
I looked at the code.<br /><br />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.<br /><br />You need something that checks the end of the URL, or looks for the parameters passed, before executing the reload.<br /><br />Does that make sense?<!--content-->
<!--fonto:Verdana--><span style="font-family:Verdana"><!--/fonto--><!--sizeo:3--><span style="font-size:12pt;line-height:100%"><!--/sizeo--><!--coloro:#336699--><span style="color:#336699"><!--/coloro--><br /><br />that makes sense, but I wouldn't know how to do it <img src="http://www.totalchoicehosting.com/forums/style_emoticons/default/huh.gif" style="vertical-align:middle" emoid=":huh:" border="0" alt="huh.gif" /> <br /><br />It's like my Spanish, I can read it but I can't write it. <img src="http://www.totalchoicehosting.com/forums/style_emoticons/default/tongue.gif" style="vertical-align:middle" emoid=":P" border="0" alt="tongue.gif" /> <br /><br />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:<br /><!--c1--><div class='codetop'>CODE</div><div class='codemain'><!--ec1--><a href=http://www.totalchoicehosting.com/forums/lofiversion/index.php/"index2.html" onClick="setCookie('Preference','index2.html',365)">flash version</a><!--c2--></div><!--ec2--><br />so the pages link AND the preference is set. but I guess that wouldn't work when the user returns on another visit (?). <br /><br />meh,<br />!!blue<br /><!--sizec--></span><!--/sizec--><!--colorc--></span><!--/colorc--><!--fontc--></span><!--/fontc--><!--content-->
 
Back
Top