Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > q - how to call a function once from onLoad

Reply
Thread Tools

q - how to call a function once from onLoad

 
 
shree
Guest
Posts: n/a
 
      07-28-2005
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>

 
Reply With Quote
 
 
 
 
Lee
Guest
Posts: n/a
 
      07-28-2005
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.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
calling javascript function once - from the <body onload="myfunction"> tag lwhitb1 Javascript 1 11-22-2005 11:31 PM
calling javascript function once - from the <body onload="myfunction"> tag lwhitb1 Javascript 1 11-22-2005 11:22 PM
base.OnLoad called, OnLoad not executed Fabio R. ASP .Net 0 03-25-2005 01:21 PM
window.onload and body.onload differences David Otton Javascript 2 11-04-2004 04:34 PM



Advertisments