User Name Password Register
DaniWeb IT Discussion Community
All
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
Reply
Join Date: Oct 2005
Posts: 236
Reputation: Inny is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 5
Inny's Avatar
Inny Inny is offline Offline
Posting Whiz in Training

Quick Help for Cookie script?

  #1  
Jan 20th, 2007
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.

<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>
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Jul 2006
Location: Deptford, London
Posts: 964
Reputation: MattEvans has a spectacular aura about MattEvans has a spectacular aura about 
Rep Power: 5
Solved Threads: 48
Moderator
Featured Poster
MattEvans's Avatar
MattEvans MattEvans is online now Online
Posting Shark

Re: Quick Help for Cookie script?

  #2  
Jan 20th, 2007
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)
If it only works in Internet Explorer; it doesn't work.
Reply With Quote  
Join Date: Oct 2005
Posts: 236
Reputation: Inny is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 5
Inny's Avatar
Inny Inny is offline Offline
Posting Whiz in Training

Re: Quick Help for Cookie script?

  #3  
Jan 21st, 2007
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>






Originally Posted by MattEvans View Post
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)
Reply With Quote  
Join Date: Jul 2006
Location: Deptford, London
Posts: 964
Reputation: MattEvans has a spectacular aura about MattEvans has a spectacular aura about 
Rep Power: 5
Solved Threads: 48
Moderator
Featured Poster
MattEvans's Avatar
MattEvans MattEvans is online now Online
Posting Shark

Re: Quick Help for Cookie script?

  #4  
Jan 21st, 2007
Sorry, I missed a bit of code you wrote in the original post:

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.
Reply With Quote  
Join Date: Oct 2005
Posts: 236
Reputation: Inny is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 5
Inny's Avatar
Inny Inny is offline Offline
Posting Whiz in Training

Re: Quick Help for Cookie script?

  #5  
Jan 21st, 2007
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>
Reply With Quote  
Join Date: Jul 2006
Location: Deptford, London
Posts: 964
Reputation: MattEvans has a spectacular aura about MattEvans has a spectacular aura about 
Rep Power: 5
Solved Threads: 48
Moderator
Featured Poster
MattEvans's Avatar
MattEvans MattEvans is online now Online
Posting Shark

Re: Quick Help for Cookie script?

  #6  
Jan 21st, 2007
this line:
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.
Reply With Quote  
Join Date: Oct 2005
Posts: 236
Reputation: Inny is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 5
Inny's Avatar
Inny Inny is offline Offline
Posting Whiz in Training

Re: Quick Help for Cookie script?

  #7  
Jan 21st, 2007
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

<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....
Reply With Quote  
Join Date: Jul 2006
Location: Deptford, London
Posts: 964
Reputation: MattEvans has a spectacular aura about MattEvans has a spectacular aura about 
Rep Power: 5
Solved Threads: 48
Moderator
Featured Poster
MattEvans's Avatar
MattEvans MattEvans is online now Online
Posting Shark

Re: Quick Help for Cookie script?

  #8  
Jan 21st, 2007
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...
Last edited by MattEvans : Jan 21st, 2007 at 11:44 pm.
If it only works in Internet Explorer; it doesn't work.
Reply With Quote  
Join Date: Oct 2005
Posts: 236
Reputation: Inny is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 5
Inny's Avatar
Inny Inny is offline Offline
Posting Whiz in Training

Re: Quick Help for Cookie script?

  #9  
Jan 22nd, 2007
Try to save this wordpad to see how its layed out, it just goes one line when cut n pasted here

<a href="http://www.fileden.com/files/2006/9/25/238265/splasher.rtf" title="splasher.rtf"> splasher.rtf </a>

i think this is right now but its still not working, I have grip marks on my mouse!
Reply With Quote  
Join Date: Jul 2006
Location: Deptford, London
Posts: 964
Reputation: MattEvans has a spectacular aura about MattEvans has a spectacular aura about 
Rep Power: 5
Solved Threads: 48
Moderator
Featured Poster
MattEvans's Avatar
MattEvans MattEvans is online now Online
Posting Shark

Re: Quick Help for Cookie script?

  #10  
Jan 22nd, 2007
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:

"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.
Attached Files
File Type: txt splasher.txt (1.4 KB, 1 views)
If it only works in Internet Explorer; it doesn't work.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb JavaScript / DHTML / AJAX Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the JavaScript / DHTML / AJAX Forum

All times are GMT -4. The time now is 2:46 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC