469,579 Members | 1,281 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,579 developers. It's quick & easy.

q - how to call a function once from onLoad

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>

Jul 28 '05 #1
2 2393
Lee
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.

Jul 28 '05 #2
shree wrote:
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.


And if the user has disabled javascript in their browser then they get to
bypass the login screen.

If you want to check for login credentials then do it at the server, no
javascript required.
Jul 29 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Larry R Harrison Jr | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.