q - how to call a function once from onLoad

Discussion in 'Javascript' started by shree, Jul 28, 2005.

  1. shree

    shree Guest

    Hi all,

    I'm trying to call javascript function via onLoad but running into a
    problem. I would appreciate any help and suggestion.
    My application is as follows. When a page loads, I want to check
    whether the vistor has the site's cookie present in their browser. If
    its not present, then to redirect user to a login page (script). If
    the cookie is present, then the rest of the html page should load.

    I'm able to get function to detect cookie's presence and direct to
    login page correctly when cookie is not present.

    If however, cookie is already present, since I'm using onLoad event,
    the page is continuously getting loaded and this is causing the browser
    to crash. I'm running windows xp, IE 6.0

    question/request - can anyone suggest a way to call js function just
    once via onLoad. Or any other way to load a function once without
    having the user to perform any action as the page loads.

    Thank you for your time,
    Shree




    <html>
    <head>
    <title>Ex_Detect_Cookie</title>
    <meta http-equiv="Content-Type" content="text/html;
    charset=iso-8859-1">

    <script language="JavaScript">

    function DetectCookie() {
    var tmpcookie = new Date();
    chkcookie = (tmpcookie.getTime() + '');
    document.cookie = "chkcookie=" + chkcookie + "; path=/";
    if (document.cookie.indexOf(chkcookie,0) < 0) {//cookie is present
    //if (document.cookie && document.cookie != "") {//cookie present
    and is not null
    var CurrentPage = document.location.href;
    window.location = CurrentPage;
    alert ("cookie is present, no need to direct user to login
    page");
    }
    else {
    //cookie is not present, redirect user to authentication page.
    alert ("cookie is not present, redirect user to login page");
    function LaunchCGIprogramText() {
    var holderText = new Image();
    holderText.src = "http://www.open365test.net/login.pl?" +
    document.location.href;
    window.location = "http://www.open365test.net/login.pl?" +
    document.location.href;
    }
    LaunchCGIprogramText();
    }
    }

    //-->
    </script>
    </head>

    <body onLoad= 'DetectCookie()'>
    <p>Print hello.
    </body>
    </html>
     
    shree, Jul 28, 2005
    #1
    1. Advertising

  2. shree

    Lee Guest

    shree said:

    >question/request - can anyone suggest a way to call js function just
    >once via onLoad. Or any other way to load a function once without
    >having the user to perform any action as the page loads.
    >
    >Thank you for your time,
    >Shree
    >
    >
    >
    >
    ><html>
    ><head>
    ><title>Ex_Detect_Cookie</title>
    ><meta http-equiv="Content-Type" content="text/html;
    >charset=iso-8859-1">
    >
    ><script language="JavaScript">
    >
    >function DetectCookie() {
    > var tmpcookie = new Date();
    > chkcookie = (tmpcookie.getTime() + '');
    > document.cookie = "chkcookie=" + chkcookie + "; path=/";
    > if (document.cookie.indexOf(chkcookie,0) < 0) {//cookie is present
    > //if (document.cookie && document.cookie != "") {//cookie present
    >and is not null
    > var CurrentPage = document.location.href;
    > window.location = CurrentPage;
    > alert ("cookie is present, no need to direct user to login
    >page");
    > }


    So you're logic says:

    if the user is logged in
    go to the current page
    else
    go to the login page.

    It should be:

    if the user is not logged in
    go to the login page

    There is absolutely no reason to set the window.location value to
    the current page. You're already there.
     
    Lee, Jul 28, 2005
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Fabio R.
    Replies:
    0
    Views:
    845
    Fabio R.
    Mar 25, 2005
  2. David Otton

    window.onload and body.onload differences

    David Otton, Nov 4, 2004, in forum: Javascript
    Replies:
    2
    Views:
    620
    Martin Honnen
    Nov 4, 2004
  3. Replies:
    5
    Views:
    320
    Thomas 'PointedEars' Lahn
    May 15, 2005
  4. lwhitb1
    Replies:
    1
    Views:
    126
    Evertjan.
    Nov 22, 2005
  5. lwhitb1
    Replies:
    1
    Views:
    267
Loading...

Share This Page