Can anyone explain why this doesn't work?

  • Thread starter Iver Erling Årva
  • Start date
I

Iver Erling Årva

I have an application that uses a window.open() to open it's own main window
where all my programs takes place. I use a timeout so if nothing goes on for
15 minutes the document below is called. To log in again all the users have
to do is to click the link in the lower part of the window. This causes the
login window to show up again. The problem arise if the window that opened
this window has been closed in the meantime. To deal with this and avoid the
top.window.opener is null or not an object error I made the startLogin
function. When the original top window has been closed the alert shows up
fine and thereby indicates that the program has reached that far, but the
document.location stuff doesn't work. Nothing happens. As you can see, I've
tried several different versions (and others too), but none of them seem to
work. Can anyone tell me what I'm doing wrong?

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>

<head>
<title>Logged Out</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<link href="Styles/StylesMenu.css" rel="stylesheet" type="text/css">


<script type=text/javascript>
function startLogin(){
if (top.window.opener){
top.window.opener.location="Default.htm";
top.window.close();
}
else {
alert("It get's to this point and then nothing more happens!");
self.document.location="Default.htm";
self.document.location.href("Default.htm");
document.location="Default.htm";
location="Default.htm";
}
}
</script>

</head>

<body onload="javascript:window.status='The system logged you out!'">
<form>
<table align="center" border="0">
<tr>
<td><div align="center"><img border="6" src="images/Trwww2.gif"
ALIGN="MIDDLE"></div>
</td>
</tr>
</table>
<h1 align="center">You are logged out!</h1>
</p>
<div align="center"><font size="2"><a href="javascript:void(0);"
onclick="startLogin();">Log in again</a></font></div>


</form>
</body>
</html>

Thanks for any help!
(e-mail address removed)
 
K

kaeli

[email protected] enlightened us said:
Can anyone tell me what I'm doing wrong?

Classic mistake.
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

Um, quirks mode?
Just an aside. Nothing to do with the error.
else {
alert("It get's to this point and then nothing more happens!");
self.document.location="Default.htm";

You just changed the location of the current document. Therefore, no more
script is present to execute (unless there's some in default.htm), as it got
wiped out by the new page.


--
 
I

Iver Erling Aarva

You just changed the location of the current document. >Therefore, no more
script is present to execute (unless there's some in >default.htm), as it got
wiped out by the new page.

but although I set self.document.location to Default.htm, Default.htm
never shows up in the browser! Shouldn't the current window at least
show Default.htm? It doesn't. That is the problem I wanted help with ;-)

Brgds
(e-mail address removed)
 
K

kaeli

but although I set self.document.location to Default.htm, Default.htm
never shows up in the browser! Shouldn't the current window at least
show Default.htm? It doesn't. That is the problem I wanted help with ;-)

Then the line is erroring out, more than likely.

Try using one of these instead.
Why are you using top.window.opener? Is this a frameset or iframe? If not,
you can probably ditch top for cleaner code. 'top' is a window object. I
don't see the need for 'top.window'. Either use top.opener or just opener.

I don't have your whole code, so check these two versions out and use the one
that works for you.

#1:
Assumes the window running this is a frameset or in an iframe:
if (top.opener){
top.opener.location="Default.htm";
top.close();
}
else {
top.document.location.href = "Default.htm"; /* use if entire frameset
should go to default */
// self.document.location.href = "Default.htm"; /* use this one if only the
current doc should go */
}

'top' would reference the upper-most window object. If a multi-layer
frameset, you may need 'parent' instead.

#2:
Assumes this code is running from a popup that is not part of a frameset:
if (self.opener){
self.opener.location="Default.htm";
self.close();
}
else {
self.document.location.href = "Default.htm";
}

Location is technically an object. href is the property to set.

Also, this code is redundant.
<body onload="javascript:window.status='The system logged you out!'">

onload is by its very nature a script event.
<body onload="window.status='The system logged you out!'">
Note that some browsers allow users to disable changing of status bar text.

Also, this isn't cool, either.
<div align="center"><font size="2"><a href="javascript:void(0);"
onclick="startLogin();">Log in again</a></font></div>

An anchor is just that - an anchor that when clicked goes to some other
document or a place in the current document (or it names a place in a
document). In newer browsers, there is just no need to pretend it's a link
when it's really more of a button (it doesn't go anywhere - it calls a script
function). NN4 didn't support div onClick, so people got used to using
anchors to do things that they really weren't intended to do.
If you want to really do it the right way, you use a real button and style it
with CSS to look like whatever you want (you can make it look like a text
link if you desire).

So, if this is an internet app and you don't know what your users have, use:
<div align="center"><font size="2"><a href="someNonJSpage.htm"
onclick="startLogin();return false;">Log in again</a></font></div>

If this is intranet and you have to support NN4, you can use the above or
use:
<div align="center"><font size="2"><a href="#"
onclick="startLogin();return false;">Log in again</a></font></div>

The 'return false' prevents the link from being followed when the function is
done being run.

If this is intranet and your users have modern browsers, NN6+/IE5
+/Mozilla/Opera6+, use:
<div align="center"><font size="2"><div onclick="startLogin();">Log in again
</div></font></div>
(name and style div as desired)

or:

<div align="center"><input type="button" value="Log In Again"
onClick="startLogin()"></div>
(again, style as desired)

HTH

--
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,990
Messages
2,570,211
Members
46,796
Latest member
SteveBreed

Latest Threads

Top