PDA

View Full Version : Javascript onload


simon315
12-22-2003, 01:46 AM
Hi. I have a very urgent question that I need to figure out ASAP. This is very urgent. I have a page of mine that pops up when a user clicks on a link. The pop-up is a small login page. Once the user is authenticated, the page is supposed to close and the main page is supposed to load a different page. So far, I've got everything working well except the past where it swithces the main page. On the main page I've created the following copy to create the pop-up:

function opensmall(url)
{
nwW = window.open(url,"opensmall","toolbar=no,scrollbars=no,width=300,height=200")
self.name = "main";
}

This code for the closing of the authentication is as follows:

<%
dim conn
dim strconn

strconn = "DRIVER=Microsoft Access Driver (*.mdb);DBQ=" & _
Server.MapPath("../database/users.mdb") 'change the path as necessary

set conn = server.createobject("adodb.connection")
conn.open strconn

'Replace single quotes in username/password with two single quotes
'to protect from SQL Injection Attack
Username = Replace(Request("username"), "'", "''")
Password = Replace(Request("password"), "'", "''")

SQL = "SELECT * FROM clients WHERE username = '" & username & "'" & _
"AND password ='" & password & "'"
set oRs = conn.Execute(SQL)

If oRs.EOF then
Response.Redirect("../extranet/login_failure.htm")
Else
session("ID") = "extranet_session" 'any word you'd like
End If

Set conn = Nothing
Set oRs = Nothing
%>
<HTML>
<HEAD>
<TITLE></TITLE>
<SCRIPT language="JavaScript">
onload="window.location.reload('../extranet/clienta/test.asp','main')"
</SCRIPT>
</HEAD>
<BODY>
<SCRIPT language="JavaScript">
var gWindowCloseWait = 0;

function SetupWindowClose()
{
window.setTimeout("window.close()",gWindowCloseWait*1000);
}

var gSafeOnload = new Array();
function SafeAddOnload(f)
{
isMac = (navigator.appVersion.indexOf("Mac")!=-1) ? true : false;
IEmac = ((document.all)&&(isMac)) ? true : false;
IE4 = ((document.all)&&(navigator.appVersion.indexOf("MSIE 4.")!=-1)) ? true : false;
if (IEmac && IE4)
{
window.onload = SafeOnload;
gSafeOnload[gSafeOnload.length] = f;
}
else if (window.onload)
{
if (window.onload != SafeOnload)
{
gSafeOnload[0] = window.onload;
window.onload = SafeOnload;
}
gSafeOnload[gSafeOnload.length] = f;
}
else
window.onload = f;
}
function SafeOnload()
{
for (var i=0;i<gSafeOnload.length;i++)
gSafeOnload[i]();
}

SafeAddOnload(SetupWindowClose);
</SCRIPT>
</BODY>
</HTML>

The lcosing works, but the loading of the other page does not. Can someone please help me??? I thought an onload command would work, but maybe it doesn't please help.

Simon315

ucm
12-22-2003, 02:18 AM
try having the server send this script to the very end of the log-in page ( only if the log-in information is correct and authenticated )


it looks like you're writing in asp, in more familuar with php but the idea is the same:

<%

... HERE - use an if statement or block to determine ...
... if the user's log-in information is authenticated ...
... and if so to then print ( or echo ) the following ...
... script to the page ...

<script language="JavaScript">
var gWindowCloseWait = 0;

function SetupWindowClose()
{
window.setTimeout("window.close()",gWindowCloseWait*1000);
}
</script>

... HERE - put an else condition statement or block ...
... that will print the following code if the log-in ...
... information is not authenticated ...

<span style="color:red;font-size:12px;">Sorry, your log-in information was not correct, please try again...</span>

%>


of course change the incorrect log ing to whatever you want...

but the idea here is to have the server-side script print the javascript that will wait for 1 second and then close the window... in fact, you shouldn't really need it to wait at all and just call window.close() if you want...

hope this helps :)

simon315
12-22-2003, 02:32 AM
Thanks for the reply. My problem is not what to do if the user is not authenticated. that works great. If the user is not authenticated, the pop-up does not close and the user is asked to try again. my problem is have the main page (not the pop-up, but the page that opened the pop-up) change when a user IS authenticated. That's my problem. Thanks again!

Simon315

ucm
12-22-2003, 02:39 AM
ohhh!!! ok...


what you're looking for is the window.opener reference property...

when you are ready to access the main page, use window.opener like:


window.opener.location='gotime.asp';

simon315
12-22-2003, 02:55 AM
FANTASTIC!!!! Thanks so much!

simon315
12-22-2003, 02:59 AM
Now what happens if the user bookmarks the certain page (the page that is switched after authentication)? Well first of all the user is brough to a login page, but after they are authenticated, I receive an error. What happens if the page that needs to be refreshed is not the "opener" what could I use instead?

ucm
12-22-2003, 02:59 AM
any time :)

simon315
12-22-2003, 11:24 AM
Does anyone know of a way to make the page change like I mentioned earlier without using the window.opener reference?

Willy Duitt
12-22-2003, 11:40 AM
Pick One: :D

location.replace
window.replace
location.href

....Willy

ucm
12-22-2003, 03:17 PM
one thing you could do is make 2x login pages, one for the little window that pops up and then another for when they refresh their page...

but that gets a little more complicated than you need to...

granted it's cool to use a pop up window for the log-in page but wouldn't it be better to have the log-in page come up in the same browser window when they click a bookmark? this way you could use willy's examples to change the page to where it needs to go after they re-log-in and then you wouldn't have to deal with the popup at all :D

think, how these forums work... you go to a page but you're not logged in so you have to log in before you can post or pm etc... so that location gets saved to a hidden input tag like <input type="hidden" name="thepagetheywanttouse" value=""> and then set the value to your choice of willy's examples... the cool thing is that the thepagetheywanttouse ( at least in php, im not sure about asp ) would get to the log-in page as $thepagetheywanttouse which you could then use to send the user to after their log-in information is authenticated...

there'e a ton of ways to go about it, these are just some :)