Jump to content
xisto Community
Sign in to follow this  
Cerebral Stasis

A Cookie Question. How would I make one-time warning pages?

Recommended Posts

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:

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

Share this post


Link to post
Share on other sites
<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.)

<script language='JavaScript'>function setCookie(page){ //This is the function that sets the cookiesvar date = new Date(); //A date-varvar 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 validvar expires = "; expires="+date.toGMTString(); //We're going to need this for the cookiedocument.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 cookiesif(!document.cookie) return; //If no cookie: returnvar cookie = document.cookie; //Getting the data from the cookie to a varvar cookie = cookie.split("="); //Splitting the data at '='j=false; //This will be clear a few lines laterfor(i=0;i<cookie.length;i++){ //A for-loop that will run once for each piece of the array cookieif(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.

Share this post


Link to post
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
Sign in to follow this  

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.