Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

redirect cookie

Status
Not open for further replies.

struth

Programmer
Aug 26, 2001
114
GB
I'm just starting out with javascript and I am trying to add a redirect to 2 buttons on an index page so that thereafter if they clicked button (a) they are redirected to page (a) and if they clicked (b) they go to (b) when they access the site.

I have been working with the cookie functions from and using window.location.href but with no luck as yet.

Can you help?

Many thanks
 
Using the cookie functions from webreference.com, I was able to set up, what I believe to be, what you want to accomplish:
<html>
<head>
<SCRIPT LANGUAGE=&quot;JavaScript&quot;>
<!--

// name - name of the cookie
// value - value of the cookie
// [expires] - expiration date of the cookie (defaults to end of current session)
// [path] - path for which the cookie is valid (defaults to path of calling document)
// [domain] - domain for which the cookie is valid (defaults to domain of calling document)
// [secure] - Boolean value indicating if the cookie transmission requires a secure transmission
// * an argument defaults when it is assigned null as a placeholder
// * a null placeholder is not required for trailing omitted arguments
function setCookie(name, value, expires, path, domain, secure) {
var curCookie = name + &quot;=&quot; + escape(value) +
((expires) ? &quot;; expires=&quot; + expires.toGMTString() : &quot;&quot;) +
((path) ? &quot;; path=&quot; + path : &quot;&quot;) +
((domain) ? &quot;; domain=&quot; + domain : &quot;&quot;) +
((secure) ? &quot;; secure&quot; : &quot;&quot;);
document.cookie = curCookie;
}

// name - name of the desired cookie
// * return string containing value of specified cookie or null if cookie does not exist
function getCookie(name) {
var dc = document.cookie;
var prefix = name + &quot;=&quot;;
var begin = dc.indexOf(&quot;; &quot; + prefix);
if (begin == -1) {
begin = dc.indexOf(prefix);
if (begin != 0) return null;
} else
begin += 2;
var end = document.cookie.indexOf(&quot;;&quot;, begin);
if (end == -1)
end = dc.length;
return unescape(dc.substring(begin + prefix.length, end));
}

// name - name of the cookie
// [path] - path of the cookie (must be same as path used to create cookie)
// [domain] - domain of the cookie (must be same as domain used to create cookie)
// * path and domain default if assigned null or omitted if no explicit argument proceeds
function deleteCookie(name, path, domain) {
if (getCookie(name)) {
document.cookie = name + &quot;=&quot; +
((path) ? &quot;; path=&quot; + path : &quot;&quot;) +
((domain) ? &quot;; domain=&quot; + domain : &quot;&quot;) +
&quot;; expires=Thu, 01-Jan-70 00:00:01 GMT&quot;;
}
}

// date - any instance of the Date object
// * hand all instances of the Date object to this function for &quot;repairs&quot;
function fixDate(date) {
var base = new Date(0);
var skew = base.getTime();
if (skew > 0)
date.setTime(date.getTime() - skew);
}

// -->
</SCRIPT>

<script language=&quot;javascript&quot;>
var now = new Date();

fixDate(now);
now.setTime(now.getTime() + 365 * 24 * 60 * 60 * 1000);

var defpage = getCookie(&quot;startpage&quot;);

if(defpage) {
parent.location = defpage;
}
</script>
</head>
<body>
<form name=&quot;selectpage&quot;>
<input type=&quot;button&quot; name=&quot;s1&quot; value=&quot;Location A&quot; onClick=&quot;setCookie('startpage', 'a.html', now, '/');&quot;>
<input type=&quot;button&quot; name=&quot;s2&quot; value=&quot;Location B&quot; onClick=&quot;setCookie('startpage', 'b.html', now, '/');&quot;>
</form>
</body>
</html>


I then created to basic files, one called a.html and the other called b.html.

I clicked on the Location B button and when I hit refresh, I was automatically redirected to b.html (Location B).

Chad. ICQ: 54380631
 
That did it ...perfect.

Many thanks
Struth
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top