| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
Redirect By a Javascript Cookie
I've searched the net for a javascript that writes a cookie on a users computer after going to a certain page. In other words when a page loads a javascript embedded in the <head>....</head> section automatically writes a cookie. I've found one here URL and though it is not exactly what I was looking for it serves the purpose.
The only problem is that it is "path dependent". What I want is the cookie to work within the entire domain. The Purpose That it Serves I have a query form on the site and after filled out and submitted the script redirects to a chosen page. This page should write the "global" cookie and after the user visits some other page (other than the original that wrote the cookie) with the same cookie script in it it should redirect to a defined page. The url of the page that writes the cookie: URL the cookie is written only after the user clicks on the login button! The url of the page that should be redirected if the cookie exists URL Here's the script. Once again! The code works perfectly there's nothing wrong with. I just want it to work within the entire domain. <SCRIPT LANGUAGE="JavaScript"> <!-- Begin 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) { var argv = SetCookie.arguments; var argc = SetCookie.arguments.length; var expires = (argc > 2) ? argv[2] : null; var path = (argc > 3) ? argv[3] : null; var domain = (argc > 4) ? argv[4] : null; var secure = (argc > 5) ? argv[5] : false; document.cookie = name + "=" + escape (value) + ((expires == null) ? "" : ("; expires=" + expires.toGMTString())) + ((path == null) ? "" : ("; path=" + path)) + ((domain == null) ? "" : ("; domain=" + domain)) + ((secure == true) ? "; secure" : ""); } function DeleteCookie () { var exp = new Date(); exp.setTime (exp.getTime() - 1000000000); // This cookie is history var cval = GetCookie ('FreeStuffL'); document.cookie ='FreeStuffL' + "=" + cval + "; expires=" + exp.toGMTString(); } function cookieCreater () { if(GetCookie('FreeStuffL') == null) { var FreeStuffL_Name = prompt ("What name do you want to go by?", "" ); if (FreeStuffL_Name != null && FreeStuffL_Name != "") { var expdate = new Date (); expdate.setTime(expdate.getTime() + (24 * 60 * 60 * 1000 * 365)); SetCookie('FreeStuffL', FreeStuffL_Name, expdate); alert ("You now are logged in! From now on, when you come to this page, you will be forwarded to the Password Protected Members-Only Area. Please do not tell anyone your entry code. At this new page, you will be shown a list of functions. Have fun!"); location.href = document.login2.go.value; } } else { DeleteCookie (); cookieCreater () } } if(GetCookie('FreeStuffL') != null) { location.href="cookie-in.html" } function check() { var tester = document.login.numOne.value + document.login.numTwo.value; if (tester == "") { alert ("I'm sorry, that code is not correct."); } else { if (tester == document.login.numThree.value) { alert ("That is correct!"); cookieCreater (); } else { alert ("Nope!"); } } } // End --> </SCRIPT> Now the form that goes in the <body>....</body> section: <FORM name='login'> Enter your login codes: <input TYPE='text' NAME='numOne' SIZE=15 value='java'> <input TYPE='text' NAME='numTwo' SIZE=15 value='script' > <input TYPE='text' NAME='numThree' SIZE=15 value='javascript' > <input TYPE='button' VALUE='Login' ONCLICK='check()'> </FORM> <FORM name="login2"> <input type=hidden name="go" value="cookie-in.html"> </FORM> I'd be really grateful if someone could help me with this. I tried simply deleting the highlighted areas above but it seems it works differently. |
|
#2
|
|||
|
|||
|
Is there a reason you're opting not to write the cookie using PHP or ASP or something similar? It's much easier to do that, and then you could simply do the redirect using javascript. This would also be more universally usable since not everyone has javascript enabled browsers.
|
|
#3
|
|||
|
|||
|
There is a reason. If I knew how to writ a script I wouldn't search the net for it. Belive it or not I spent almost 15 hours searching and this script is the most reliable for the purpose I want.
|
|
#4
|
|||
|
|||
|
I still cant see why your using javascript instead of sumthing like php and asp as kurt said, it would be much easier and possibly less code to actually write, you could even download a script already made for this purpose. Take a look at sites like e.g. www.hotscripts.com etc and see if you cant use one of them would be much easier
|
|
#5
|
|||
|
|||
|
Is there a way to change the access codes, I cannot figure out how!
|
|
#6
|
|||
|
|||
|
How do you change the passwords!
|
![]() |
| Viewing: Tutorialized Forums > Web Design & Development > Javascript > Redirect By a Javascript Cookie |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|
|