•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the JavaScript / DHTML / AJAX section within the Web Development category of DaniWeb, a massive community of 427,324 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,953 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our JavaScript / DHTML / AJAX advertiser: Lunarpages Web Hosting
Views: 1617 | Replies: 10
![]() |
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.
<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>•
•
Join Date: Jul 2006
Location: Deptford, London
Posts: 964
Reputation:
Rep Power: 5
Solved Threads: 48
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:
var Splash = GetCookie('SplashSkip');
ReDirect('/welcome');To:
var Splash = GetCookie('SplashSkip');
if(Splash == null){
ReDirect('/welcome');
}Remember to clear your cookies to test (I find Opera is best for testing cookies)
If it only works in Internet Explorer; it doesn't work.
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?
<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:
var Splash = GetCookie('SplashSkip'); ReDirect('/welcome');
To:
var Splash = GetCookie('SplashSkip'); if(Splash == null){ ReDirect('/welcome'); }
Remember to clear your cookies to test (I find Opera is best for testing cookies)
•
•
Join Date: Jul 2006
Location: Deptford, London
Posts: 964
Reputation:
Rep Power: 5
Solved Threads: 48
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:
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 1:23 pm.
If it only works in Internet Explorer; it doesn't work.
is this correct then? Ive entered the url to direct to in the right part?
<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>•
•
Join Date: Jul 2006
Location: Deptford, London
Posts: 964
Reputation:
Rep Power: 5
Solved Threads: 48
this line:
should be:
and if that's the page you want to redirect to; this line:
should be:
function ReDirect (http://download3-1.files-upload.com/2007-01/20/22/RP.html) {should be:
function ReDirect (URL) {and if that's the page you want to redirect to; this line:
ReDirect('/welcome');should be:
ReDirect('http://download3-1.files-upload.com/2007-01/20/22/RP.html'); If it only works in Internet Explorer; it doesn't work.
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
<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 10:43 pm. Reason: still not quite right....
•
•
Join Date: Jul 2006
Location: Deptford, London
Posts: 964
Reputation:
Rep Power: 5
Solved Threads: 48
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 21st, 2007 at 11:44 pm.
If it only works in Internet Explorer; it doesn't work.
•
•
Join Date: Jul 2006
Location: Deptford, London
Posts: 964
Reputation:
Rep Power: 5
Solved Threads: 48
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:
"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 8:50 pm.
If it only works in Internet Explorer; it doesn't work.
![]() |
•
•
•
•
•
•
•
•
DaniWeb JavaScript / DHTML / AJAX Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
- 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?


Linear Mode