472,782 Members | 2,916 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,782 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 2567
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: Rina0 | last post by:
Cybersecurity engineering is a specialized field that focuses on the design, development, and implementation of systems, processes, and technologies that protect against cyber threats and...
0
by: erikbower65 | last post by:
Using CodiumAI's pr-agent is simple and powerful. Follow these steps: 1. Install CodiumAI CLI: Ensure Node.js is installed, then run 'npm install -g codiumai' in the terminal. 2. Connect to...
0
linyimin
by: linyimin | last post by:
Spring Startup Analyzer generates an interactive Spring application startup report that lets you understand what contributes to the application startup time and helps to optimize it. Support for...
0
by: erikbower65 | last post by:
Here's a concise step-by-step guide for manually installing IntelliJ IDEA: 1. Download: Visit the official JetBrains website and download the IntelliJ IDEA Community or Ultimate edition based on...
0
by: kcodez | last post by:
As a H5 game development enthusiast, I recently wrote a very interesting little game - Toy Claw ((http://claw.kjeek.com/))。Here I will summarize and share the development experience here, and hope it...
14
DJRhino1175
by: DJRhino1175 | last post by:
When I run this code I get an error, its Run-time error# 424 Object required...This is my first attempt at doing something like this. I test the entire code and it worked until I added this - If...
0
by: lllomh | last post by:
How does React native implement an English player?
0
by: Mushico | last post by:
How to calculate date of retirement from date of birth
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...

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.