Jump to content

Redirect from Cookie


Skemcin

Recommended Posts

I'm back with my environmental restriction nightmares. Here is my latest . . . [1] Assume I have the equivilant of a server side include, I need it to create a cookie called redirect with a value of homepageA or homepageB or whatever I set it to. This needs to be done without using the <body> tag in any way shape or form.[2] Given that that cookie exists, I need some code that I can put on a page that when linked to will redirect the person to xxxxxx.xxx?page=[cookie value]Here is what I have so far, everything being manually triggered. Remember, I have no access to the <body> tag and the cookie and the redirect have to be in javascript.Thanks in advance.

<script type="text/javascript">function createCookie(name,value,days){	if (days)	{		var date = new Date();		date.setTime(date.getTime()+(days*24*60*60*1000));		var expires = "; expires="+date.toGMTString();	}	else var expires = "";	document.cookie = name+"="+value+expires+"; path=/";}function readCookie(name){	var nameEQ = name + "=";	var ca = document.cookie.split(';');	for(var i=0;i < ca.length;i++)	{		var c = ca[i];		while (c.charAt(0)==' ') c = c.substring(1,c.length);		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);	}	return null;}function eraseCookie(name){	createCookie(name,"",-1);}</script><a href="java script: createCookie('redirect','homepageA',0);">Set A Cookie</a><br /><a href="java script: createCookie('redirect','homepageB',0);">Set B Cookie</a><br /><a href="java script: readCookie('redirect');">Read Cookie</a><br />

When you load this code into page and safe it locally, it works the way it is coded. But, what I am struggling with is two things:[1] automating the cookie creating without the use of a body tag[2] inserting the rediection into the script so that I can use the same logic used in resolving my first issue to read the cookie and redirect the user accordinglyMake any sense?

Link to comment
Share on other sites

Ok, so I got the setting part done using this:

<div onmousemove="createCookie('redirect','homepageA',0);" />

and

<div onkeypress="createCookie('redirect','homepageA',0);" />

but now I need to figure out how to ge the redirect to happen when the page loads without having access to the body tag . . .

Link to comment
Share on other sites

in the script you can specify the onload event.

onload=function functionName(){......}

is the same as

<body onload="functionName()">

or you could grab the body tag like this

var bodyTag = document.getElementsByTagName('body')[0];bodyTag.onload = function(){functionName()}

Link to comment
Share on other sites

but the function is shared by all the sites I am wokring on, so I need some script that will look like this:

<a href="javascript: readCookie('redirect');">Read Cookie</a><br />

but execute automatically. So, are you saying this will work?

<script language="javascript" type="text/javascript">onload = function readCookie(redirect)</script>

cuz that doesn't :)

Link to comment
Share on other sites

So you can't use window.onload? Does that access the body tag?

<script language="javascript" type="text/javascript">window.onload = function readCookie(redirect)</script>

Link to comment
Share on other sites

but now I need to figure out how to ge the redirect to happen when the page loads without having access to the body tag . . .
try this code skemcin
<script>function init() {alert("You are being re-directed");window.location="http://www.google.com";}window.onload = init; </script>

isn't onload= the same as window.onload= ???  :)

When used in the code above yes.window.onload = window.alert("hi");can be shorthanded to this without using window.onload = alert("hi");
Link to comment
Share on other sites

thanks for all the input, here is what I was able to piece together given the code presented and my sick, twisted, yet creative mind:this is the only content on the page that performs the redirection

<script type="text/javascript">function readCookie(name){	var nameEQ = name + "=";	var ca = document.cookie.split(';');	for(var i=0;i < ca.length;i++)	{  var c = ca[i];  while (c.charAt(0)==' ') c = c.substring(1,c.length);  if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);	}	return null;}onload = function exeRedirect(name){	var goto=readCookie(name);	if (goto=='null')	document.location="xxxxxx.xxx?page=xxxxxx";	else	document.location="xxxxxx.xxx?page="+goto+"";	}</script>

Essentially, I just used aspnetguy's suggestion and then created another function to read the results of my first function, then I use that as my url parameter in a document.location. that resolved my second issue. I have also figured out an acceptable way to resolve my first, related issue. I am calling an external js file like stated before, defining the createCookie, readCookie, and eraseCookie functions, then use the following two tags in the applications equivilant of an SSI:

<div onmousemove="createCookie('redirect','homepageA',0);" /><div onkeypress="createCookie('redirect','homepageA',0);" />

So the divs (loaded on every page) call my functions which create my cookie. Then, whenever one of my shared sites comes to the parent site, the parent site can execute the redirect script since the cookie has been set.I'll test it a little more thoroughly tomorrow, thanks for all the help.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...