| | |
Quick Help for Cookie script?
Please support our JavaScript / DHTML / AJAX advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
![]() |
hello again Dani web folk, Im needing a little help with an example script. This cookie setter is prewritten but I want to set it up for use on my site but im not sure how. I want to redirect from here
http://herproom.5.forumer.com/index.php?
to here
http://download3-1.files-upload.com/.../20/22/RP.html
only once, the first time someone visits my site.
Please help me configure this code? much appreciated.
http://herproom.5.forumer.com/index.php?
to here
http://download3-1.files-upload.com/.../20/22/RP.html
only once, the first time someone visits my site.
Please help me configure this code? much appreciated.
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
<script type="text/javascript"> // adapted from http://evolt.org/article/list/20/416/ // <![CDATA[ var Splash = GetCookie('SplashSkip'); ReDirect('/welcome'); function ReDirect (URL) { SetCookie('SplashSkip','TRUE',1); if (Splash == 'TRUE') { window.location=(URL); } } function getCookieVal (offset) { var endstr = document.cookie.indexOf (";", offset); if (endstr == -1) endstr = document.cookie.length; return unescape(document.cookie.substring(offset, endstr)); } function GetCookie (name) { var arg = name + "="; var alen = arg.length; var clen = document.cookie.length; var i = 0; while (i < clen) { var j = i + alen; if (document.cookie.substring(i, j) == arg) return getCookieVal (j); i = document.cookie.indexOf(" ", i) + 1; if (i == 0) break; } return null; } function SetCookie(name, value, expDays, path, domain, secure) { // Set cookie with name, value etc provided // in function call and date from above // Number of days the cookie should persist NB expDays='' or undef. => non-persistent if (expDays != null ) { var expires = new Date(); expires.setTime(expires.getTime() + (expDays*24*60*60*1000)); } var curCookie = name + "=" + escape(value) + ((expires) ? "; expires=" + expires.toGMTString() : "") + ((path) ? "; path=" + path : "") + ((domain) ? "; domain=" + domain : "") + ((secure) ? "; secure" : ""); document.cookie = curCookie; } // ]]> </script>
Providing the cookie scipt you're using works correctly:
Change:
To:
Remember to clear your cookies to test (I find Opera is best for testing cookies)
Change:
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
var Splash = GetCookie('SplashSkip'); ReDirect('/welcome');
To:
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
var Splash = GetCookie('SplashSkip'); if(Splash == null){ ReDirect('/welcome'); }
Remember to clear your cookies to test (I find Opera is best for testing cookies)
Plato forgot the nullahedron..
Thanks, I dont think I put the urls in right place though, or something, it didnt redirect at all? was this right or did I forget something? index of?
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
<script type="text/javascript"> // adapted from http://evolt.org/article/list/20/416/ // <![CDATA[ var Splash = GetCookie('SplashSkip'); if(Splash == null){ ReDirect('/welcome'); } function ReDirect (http://herproom.5.forumer.com/index.php?) { SetCookie('SplashSkip','TRUE',1); if (Splash == 'TRUE') { window.location=(http://download3-1.files-upload.com/.../20/22/RP.html ); } } function getCookieVal (offset) { var endstr = document.cookie.indexOf (";", offset); if (endstr == -1) endstr = document.cookie.length; return unescape(document.cookie.substring(offset, endstr)); } function GetCookie (name) { var arg = name + "="; var alen = arg.length; var clen = document.cookie.length; var i = 0; while (i < clen) { var j = i + alen; if (document.cookie.substring(i, j) == arg) return getCookieVal (j); i = document.cookie.indexOf(" ", i) + 1; if (i == 0) break; } return null; } function SetCookie(name, value, expDays, path, domain, secure) { // Set cookie with name, value etc provided // in function call and date from above // Number of days the cookie should persist NB expDays='' or undef. => non-persistent if (expDays != null ) { var expires = new Date(); expires.setTime(expires.getTime() + (expDays*24*60*60*1000)); } var curCookie = name + "=" + escape(value) + ((expires) ? "; expires=" + expires.toGMTString() : "") + ((path) ? "; path=" + path : "") + ((domain) ? "; domain=" + domain : "") + ((secure) ? "; secure" : ""); document.cookie = curCookie; } // ]]> </script>
•
•
•
•
Providing the cookie scipt you're using works correctly:
Change:
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
var Splash = GetCookie('SplashSkip'); ReDirect('/welcome');
To:
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
var Splash = GetCookie('SplashSkip'); if(Splash == null){ ReDirect('/welcome'); }
Remember to clear your cookies to test (I find Opera is best for testing cookies)
Sorry, I missed a bit of code you wrote in the original post:
You do this test in the ReDirect function:
that test should be a test for Splash NOT being "TRUE". Otherwise; your redirect will only happen when the user has visited the page at least once.
The cookie code your using should return a null if the cookie isn't found; but perhaps it's better to test for it not being "TRUE" just in case.
So, the top part of your code should read:
You do this test in the ReDirect function:
if(Splash == "TRUE"){ };that test should be a test for Splash NOT being "TRUE". Otherwise; your redirect will only happen when the user has visited the page at least once.
The cookie code your using should return a null if the cookie isn't found; but perhaps it's better to test for it not being "TRUE" just in case.
So, the top part of your code should read:
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
var Splash = GetCookie('SplashSkip'); ReDirect('/welcome'); function ReDirect (URL) { if (Splash != 'TRUE') { SetCookie('SplashSkip','TRUE',1); window.location=(URL); } }
Last edited by MattEvans; Jan 21st, 2007 at 2:23 pm.
Plato forgot the nullahedron..
is this correct then? Ive entered the url to direct to in the right part?
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
<script type="text/javascript"> // adapted from http://evolt.org/article/list/20/416/ // <![CDATA[var Splash = GetCookie('SplashSkip'); ReDirect('/welcome'); function ReDirect (http://download3-1.files-upload.com/2007-01/20/22/RP.html) { if (Splash != 'TRUE') { SetCookie('SplashSkip','TRUE',1); window.location=(URL); } } function getCookieVal (offset) { var endstr = document.cookie.indexOf (";", offset); if (endstr == -1) endstr = document.cookie.length; return unescape(document.cookie.substring(offset, endstr)); } function GetCookie (name) { var arg = name + "="; var alen = arg.length; var clen = document.cookie.length; var i = 0; while (i < clen) { var j = i + alen; if (document.cookie.substring(i, j) == arg) return getCookieVal (j); i = document.cookie.indexOf(" ", i) + 1; if (i == 0) break; } return null; } function SetCookie(name, value, expDays, path, domain, secure) { // Set cookie with name, value etc provided // in function call and date from above // Number of days the cookie should persist NB expDays='' or undef. => non-persistent if (expDays != null ) { var expires = new Date(); expires.setTime(expires.getTime() + (expDays*24*60*60*1000)); } var curCookie = name + "=" + escape(value) + ((expires) ? "; expires=" + expires.toGMTString() : "") + ((path) ? "; path=" + path : "") + ((domain) ? "; domain=" + domain : "") + ((secure) ? "; secure" : ""); document.cookie = curCookie; } // ]]> </script>
this line:
should be:
and if that's the page you want to redirect to; this line:
should be:
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
function ReDirect (http://download3-1.files-upload.com/2007-01/20/22/RP.html) {
should be:
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
function ReDirect (URL) {
and if that's the page you want to redirect to; this line:
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
ReDirect('/welcome');
should be:
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
ReDirect('http://download3-1.files-upload.com/2007-01/20/22/RP.html');
Plato forgot the nullahedron..
Ahh, Thankyou! 
Edit~ ok heres what I have but it still does nothing. forgot to set cookie name, value,path etc etc ???how to do that for persistant cookie

Edit~ ok heres what I have but it still does nothing. forgot to set cookie name, value,path etc etc ???how to do that for persistant cookie
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
<script type="text/javascript"> // adapted from http://evolt.org/article/list/20/416/ // <![CDATA[var Splash = GetCookie('SplashSkip'); ReDirect('http://download3-1.files-upload.com/2007-01/20/22/RP.html'); function ReDirect (URL) { if (Splash != 'TRUE') { SetCookie('SplashSkip','TRUE',1); window.location=(URL); } } function getCookieVal (offset) { var endstr = document.cookie.indexOf (";", offset); if (endstr == -1) endstr = document.cookie.length; return unescape(document.cookie.substring(offset, endstr)); } function GetCookie (name) { var arg = name + "="; var alen = arg.length; var clen = document.cookie.length; var i = 0; while (i < clen) { var j = i + alen; if (document.cookie.substring(i, j) == arg) return getCookieVal (j); i = document.cookie.indexOf(" ", i) + 1; if (i == 0) break; } return null; } function SetCookie(name, value, expDays, path, domain, secure) { // Set cookie with name, value etc provided // in function call and date from above // Number of days the cookie should persist NB expDays='' or undef. => non-persistent if (expDays != null ) { var expires = new Date(); expires.setTime(expires.getTime() + (expDays*24*60*60*1000)); } var curCookie = name + "=" + escape(value) + ((expires) ? "; expires=" + expires.toGMTString() : "") + ((path) ? "; path=" + path : "") + ((domain) ? "; domain=" + domain : "") + ((secure) ? "; secure" : ""); document.cookie = curCookie; } // ]]> </script>
Last edited by Inny; Jan 21st, 2007 at 11:43 pm. Reason: still not quite right....
When you set that cookie, it will default to only be available to the subdomain of the page that set the cookie, and possibly only the folder that set that cookie.
A site-wide cookie should have:
Path = "/"
Domain = "yourdomain.tld" (without the www)
For persistance; set the date 100 years in the future and remember to update it if its about to expire XD
So, if you don't set the path and domain, that cookie might only be available to pages in the folder of the page that set the cookie.
Does the splash one-time only functionality work on the page that set the cookie? It is working ok on my server.
If you are entering code in one continuous line like it's coming up in your posts, it's not going to work unless you remove all the comments... (I'd advise putting the code back into different lines) The first time the javascript engine sees // it ignores all data until it reaches a line break...
A site-wide cookie should have:
Path = "/"
Domain = "yourdomain.tld" (without the www)
For persistance; set the date 100 years in the future and remember to update it if its about to expire XD
So, if you don't set the path and domain, that cookie might only be available to pages in the folder of the page that set the cookie.
Does the splash one-time only functionality work on the page that set the cookie? It is working ok on my server.
If you are entering code in one continuous line like it's coming up in your posts, it's not going to work unless you remove all the comments... (I'd advise putting the code back into different lines) The first time the javascript engine sees // it ignores all data until it reaches a line break...
Last edited by MattEvans; Jan 22nd, 2007 at 12:44 am.
Plato forgot the nullahedron..
Ok, the main problem there; was with quoting - where you tried to put the cookie domain inside the cookie setting function directly you quoted it so that a load of the script became quoted (quoted parts in <script> tags are generally treated as text, or as an error). That's quite a weird construct (multiple ternary operations concatenated to generate the cookie string). It wasn't very neatly written either.
I've attached a copy of the file that works, and you can see it working at http://www.fusiongroups.net/cookie.html.
You gotta beware of those quotes:
check out:
http://www.quirksmode.org/js/strings.html
I've attached a copy of the file that works, and you can see it working at http://www.fusiongroups.net/cookie.html.
You gotta beware of those quotes:
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
"this is a "test" is a test" = illegal, and will probably crash the script. "this 'is a 'test' is a test" = legal, even though there's an odd number of ' quotes 'this is a "test" is a test' = should be legal aswell. "this is a \"test\" is a test" = legal because the \ character means 'treat the next character as text, even if it should do something special' "this is a test' - illegal, and anything following that, until a " is reached will be treated as text, this will potentially crash the script unless there's an inverted error like this in exactly the right place.
check out:
http://www.quirksmode.org/js/strings.html
Last edited by MattEvans; Jan 22nd, 2007 at 9:50 pm.
Plato forgot the nullahedron..
![]() |
Similar Threads
- Is this possible with a cookie and script (HTML and CSS)
- log in script saves cookie (JavaScript / DHTML / AJAX)
Other Threads in the JavaScript / DHTML / AJAX Forum
- Previous Thread: Auto replace hotlinked Image Javascript?
- Next Thread: How does IE determine a popup?
| Thread Tools | Search this Thread |
ajax ajaxcode ajaxexample ajaxhelp ajaxjspservlets animate automatically beta box browser bug calendar captchaformproblem checkbox child class close column createrange() css cursor debugger dependent disablefirebug dom download dropdown editor element embed engine error events explorer ext file form forms getselection google gwt gxt hiddenvalue highlightedword hint html htmlform ie7 ie8 iframe images internet java javascript javascripthelp2020 jawascriptruntimeerror jquery jsf jsfile jump libcurl math media microsoft mimic object onmouseoutdivproblem onreadystatechange parent paypal pdf php player position post problem programming progressbar regex runtime scroll search security select session shopping size software sql text textarea unicode w3c web website window windowofwords windowsxp wysiwyg \n






