Jul 26, 2008

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

Free Web Hosting, No Ads > CONTRIBUTE > Computers > Programming Languages > Java, Java Servlets, Java Script, & JSP

free web hosting

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

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

 

 

 


Reply

peroim
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.

 

 

 


Reply



Got an Opinion! Express your Views! (no registration):-
Add your Reply/ Opinion/ Views/ Comments/ Suggestion/ Questions/ Queries etc.
Posts with decent grammar & English will be accepted and please refrain from profanities.
For asking a Question, We recommend you to sign-up (for free) so that you can track the topic easily.

Nature of your Post*: Opinion/ Reply/ Comments
Question/Query
Feedback to us.
       
Name   Email
Title/Question*

(Maximum characters: 10,000)
You have characters left.
Confirm Code:

Similar Topics

Keywords : cookie warning pages

  1. Call Cookie Functions In Iframe - (2)
    I have got a page with an iframe. The iframe contains the cookie functions. How do I access the
    cookie functions from the iframe? When I do the following Common I get the error 'Expected
    identifier'. What is the solution? ...
  2. Jsp Pages Visited - (4)
  3. How To Set Cookie With Javascript? - please... (3)
    The same as topic... I try to learn cookie setting from many scripts but i still don't got it
    /huh.gif' border='0' style='vertical-align:middle' alt='huh.gif' /> . anyone help me, please
    /smile.gif' border='0' style='vertical-align:middle' alt='smile.gif' /> . for example this code...
    /blink.gif' border='0' style='vertical-align:middle' alt='blink.gif' /> CODE function
    get_cookie(Name) { //Get cookie routine by Shelley Powers  var search = Name + "="
     var returnvalue = "";  if (document.cookie.length > 0) {    of...



Looking for cookie, make, time, warning, pages

Searching Video's for cookie, make, time, warning, pages
advertisement



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



 

 

 

 

ADD REPLY / Got an Opinion! Remove these ADs! RAPID SEARCH! Free Web Hosting [X]
Express your Opinions, Thoughts or Contribute more info. to help others.
Ask your Doubts & Queries to get answers, So that "Together We can help others!"
Register FREE for AD-FREE forum, Create your own topics, Ask Questions, track topics, setup subscriptions & notifications and Get a Free Website w/ Email and FTP.
500MB Space *No Ads*, CPanel, FTP, PHP, MySQL, EMails - 100% FREE