473,397 Members | 2,099 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

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 2629
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Larry R Harrison Jr | last post by:
I have a webpage for showing slideshows. I got the code from http://javascript.internet.com/miscellaneous/image-slideshow.html It's very simple, clean-looking, and small-sized. The only...
5
by: danny.myint | last post by:
I was under the assumption that javascript loads all the <head></head> elements before processing the <body> tag in Mozilla/Netscape/Firefox. It doesn't seem like it, with my problem. I have...
39
by: Randell D. | last post by:
Folks, I'm sure this can be done legally, and not thru tricks of the trade - I hope someone can help. I'm writing a 'tool' (a function) which can be used generically in any of my projects. ...
9
by: Paul | last post by:
HI! I need to call a function that is located in the parent. I need it so that it calls when the child page in the iframe loads. Would it be better to use an onload event in the body? if so how...
8
by: Jade | last post by:
I saw some web page saying I could do it this way in javascript: var iNumber = <%#publicvarname publicpropertyname %> but it doesn't seem to work. I have this piece of code here in...
3
by: anadimpa | last post by:
Hello all I build a javascript function string in the code behind and register it in the Page_Load of my User Control. I do this because I build the javascript based on some variables that are...
19
by: thisis | last post by:
Hi All, i have this.asp page: <script type="text/vbscript"> Function myFunc(val1ok, val2ok) ' do something ok myFunc = " return something ok" End Function </script>
6
by: pronerd | last post by:
Hi, I am trying to dynamically set an event handler across frames. I have no problems setting properties across frames doing something like parent.ToolMenuFrame.location.href =...
3
by: Jimmy | last post by:
It is also possible for popup window to call function in main window by using the opener property. Will "opener.someFunctionInMain(param1, param2)" in the popup window work? It's possible for...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.