|
|
|
|
![]() ![]() |
Jan 20 2006, 05:22 AM
Post
#1
|
|
|
Braindead by Default ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Group: Members Posts: 715 Joined: 5-November 05 From: United States of America Member No.: 13,837 |
I used some code taken from a few basic tutorials on the Internet and thrown together to create a rough script that will, hopefully, request one to click a link to enter a website, but once the link has been clicked once, a cookie will be created so that page will forward one directly to the next page.
The following is the code I have in my head tag: CODE <script LANGUAGE="JavaScript"> cookie_name = "dataCookie"; function putCookie() { if(document.cookie != document.cookie) {index = document.cookie.indexOf(cookie_name);} else { index = -1;} </SCRIPT> <script LANGUAGE="JavaScript"> cookie_name = "dataCookie"; function getName() { if(document.cookie) { index = document.cookie.indexOf(cookie_name); if (index == -1) {javascript:go ('home.htm');} } </SCRIPT> And then I have an Onload attribute that points to getName() in my body tag and a onClick attribute for each of my links that points to putCookie() Can anyone tell me what is wrong and/or fix/remake the script so it will work? Oh, and would a page the forwards in this manner break the "no forwarded pages" rule that exists for many directories? |
|
|
|
Jul 13 2006, 04:05 PM
Post
#2
|
|
|
Newbie [Level 2] ![]() ![]() Group: Members Posts: 33 Joined: 9-July 06 From: Belgium Member No.: 26,367 |
CODE <script LANGUAGE="JavaScript"> cookie_name = "dataCookie"; function putCookie() { if(document.cookie != document.cookie) <-- This is a weird piece! (*) {index = document.cookie.indexOf(cookie_name);} else { index = -1;} </SCRIPT> <script LANGUAGE="JavaScript"> cookie_name = "dataCookie"; function getName() { if(document.cookie) { index = document.cookie.indexOf(cookie_name); if (index == -1) {java script:go ('home.htm');} } </SCRIPT> (*): document.cookie will always be the same as document.cookie, so the following piece of code will never be executed. Now, what you do is looking if the cookie contains 'dataCookie', if it doesn't, it will go to 'home.htm' (I think the function go() doesn't excist, I always use 'window.location = something'.) If I see your code, I think there won't ever be a cookie. First: the code won't be executed (see (*)) and if it would be executed, it would be a session-only cookie. To be honest, if I were you, I would make this script all over again. (But I'll do it for you now.) CODE <script language='JavaScript'> function setCookie(page){ //This is the function that sets the cookies var date = new Date(); //A date-var var expires = 24*60*60*1000; //This is the time the cookie is valid in milliseconds (This is 1 day) date.setTime(date.getTime()+expires); //This is where we see when the cookie is no longer valid var expires = "; expires="+date.toGMTString(); //We're going to need this for the cookie document.cookie = "page=" + page + expires; //Filling the cookie with data (Important: this cookie needs the variable expires! window.location = page; //Redirecting to the given page } function getCookie(){ //This function will check the cookies if(!document.cookie) return; //If no cookie: return var cookie = document.cookie; //Getting the data from the cookie to a var var cookie = cookie.split("="); //Splitting the data at '=' j=false; //This will be clear a few lines later for(i=0;i<cookie.length;i++){ //A for-loop that will run once for each piece of the array cookie if(cookie[i] == "page") j=i+1; //If the piece of the array is equal to "page" our var in the first function, we save the position + 1 in the var j. Why 'i+1': this is the piece of the array that will contain the page, since we splitted at '=' } if(!j) return; //If j is not set, see the line 'j=false', return. (This is when there is a cookie, but it doesn't contain the right data. document.location = cookie[j]; //And again: redirect to the given page } window.onload = getCookie; //Makes the function getCookie() start when the page is loaded </SCRIPT> This script will check for cookies when you open the page, if the cookie is set, it will redirect to the given page. How to use it: in you page should be links, buttons, ... to the following url: 'java script:setCookie(page)'. (don't type 'page', but this should be the url you want to redirect to. Example: 'home_en.html'.) Hope this was usefull for you. |
|
|
|
![]() ![]() |
Similar Topics
|
Lo-Fi Version | Time is now: 7th October 2008 - 10:28 PM |