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/2007-01/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>

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)

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)

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);
}
}

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>

Ahh, Thankyou! :D

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>

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...

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! :D

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

Thankyou again. Unfortunately It still displays the splash page repeatedly, for some reason? Works perfectly in a testbed!

<script type="text/javascript"> 
//<![CDATA[
var Splash = GetCookie('SplashSkip'); 
ReDirect('http://download3-1.files-upload.com/2007-01/23/10/RP2.html'); 

function ReDirect (URL) { 
if (Splash != 'TRUE') { 
	SetCookie('SplashSkip','TRUE',1,'/','http://herproom.5.forumer.com'); 
	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) { 
	// 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);
	curCookie = curCookie + ((expires) ? "; expires=" + expires.toGMTString() : "");
	curCookie = curCookie + ((path) ? "; path=" + path : "");
	curCookie = curCookie + ((domain) ? "; domain=" + domain : "");
	curCookie = curCookie + ((secure) ? "; secure" : ""); 
	document.cookie = curCookie; 
} 
// ]]>
</script>
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.