View Full Version : How to reload a window from another?
Transistor
11-14-2002, 09:06 PM
Hello everyone, this is my first post! (of many, I hope).
At my site, I pop a window where the user enters his/her id & pwd.
If login is succesful, the main window should refresh to show his/her content.
I am currently using this:
window.opener.history.go(0)
The problem is that this code does not work with IE 6 in Windows.
I am developing on a Mac and this code works for all Mac browsers, but I have been told that Windows browsers have problems with this code.
Windows browers need another code? or is it there one that will work with all?
Thanks! :thumbsup:
chrismiceli
11-14-2002, 10:54 PM
if you name the parent, not the popup someting like "test", i think it is like this
parent.test.reload()
whammy
11-15-2002, 12:50 AM
Are you using a server-side language to determine whether the user has logged in successfully?
glenngv
11-15-2002, 01:19 AM
try:
if (window.opener && !window.opener.closed) window.opener.location.reload();
whammy
11-15-2002, 01:22 AM
Oh, so in IE when you close the opener it doesn't update the object itself?
Er, anyway now I'm confused... but that code looks completely redundant, so something is messed up there for sure.
Nice. :rolleyes:
glenngv
11-15-2002, 01:40 AM
you won't reload the opener window if it's closed or you will get an error :D
whammy
11-15-2002, 01:49 AM
I know that.. wouldn't you do:
if (window.opener) window.opener.location.reload();
in that case though?
that's my point actually... why wouldn't window.opener not be set to null or undefined if it doesn't exist. That is an oversight, I think.
glenngv
11-15-2002, 02:00 AM
window.opener will still be [object] even if that opener is closed.
try this 2 htm pages:
<html>
<body>
<form>
Opener Window
<p><input type="button" value="PopUp" onclick="window.open('popup.htm')">
</form>
</body>
</html>
then in popup.htm:
<html>
<body>
<form>
PopUp Window
<p>
<input type="button" value="PopUp without check" onclick="alert(window.opener);window.opener.location.reload();"><br>
<input type="button" value="PopUp with check" onclick="if (window.opener && !window.opener.closed) window.opener.location.reload();else alert('opener closed')">
</form>
</body>
</html>
try clicking the buttons in popup while its opener is closed
whammy
11-15-2002, 02:03 AM
Ok, that's what I was saying, lol.
I was testing it myself a bit... I think when the opener is closed that javascript should no longer have it as an object in its memory (therefore eliminating the need for redunancy). I personally think this is a bug in javascript and ECMAScript, and that the object should be deleted when it no longer exists.
I realize that results in [object] if it's undefined, but it just seems a poor way to program. :)
Am I totally off base here? Or would that be hard to do. I would think it would be easy... and until they fix it I will call it a bug unless you prove me wrong. LOL
How hard is it to see if the requested object exists? I know that's what your original code does. But to me it seems like that kind of check should be done in the language itself, and you shouldn't have to code for it as an eventuality.
Just my opinion, but I see a lot of areas in which ECMAscript can obviously improve when it comes to coding. I just think it's stupid to have a programmer check to see if the object exists (AND do another check) when it should be built in. :)
glenngv
11-15-2002, 02:41 AM
i think that's not a bug in javascript.
it is correct that window.opener should always return [object] even if it's closed or not (as long, of course, as the window is opened by another window).
come to think of it, if the opener is closed and it should return null or undefined (as you would want it to be), how can we check the closed property of the window.opener object to know if it's closed or not? There should be no closed property if that would be the case :D
hope that makes sense :)
whammy
11-15-2002, 02:54 AM
So in that case, why wouldn't if(!window.opener) work since that should check for the existence of the object?
I'm not at my full mental capacity at the moment, so forgive me. :)
glenngv
11-15-2002, 03:04 AM
as i said, window.opener will still be [object] even if it's closed, that means the object still exist.
so doing:
if (window.opener && !window.opener.closed) window.opener.location.reload();
is not redundant.
that will not throw an error even if the opener is closed or there is no opener at all (which happens when the current window is opened directly in the browser)
whammy
11-15-2002, 03:05 AM
Ok, I get that... but that's not a bug? :)
If the object doesn't exist OR it is closed, then it should still throw the same error, shouldn't it? I mean if you close an existing object, it should no longer exist. I still think it's a bug.
But I guess I'm picky. :) And either I STILL don't get what you're saying or I was right to begin with?!?
glenngv
11-15-2002, 03:18 AM
alert(window.opener.closed)
that should display true if the opener is closed or false if still open.
do you agree? :)
if yes, read on else stop...
if window.opener is closed and you said it should be null or undefined, then how would the alert display true since the opener is already closed
get it? :)
Transistor
11-15-2002, 05:09 PM
Interesting discussion going on here.
I'm trying:
window.opener.location.reload();
At least it works on all my Mac browsers, I'll let you know if Windows browsers like it :)
Thank you!
whammy
11-16-2002, 12:52 AM
Hmm, I get it (I think)... but If I DO get what you're saying I don't necessarily agree with the logic used when creating it. :)
if window.opener is closed - the window.opener object should cease to exist (that may not be how javascript WORKS, but that only makes sense to me anyway - why would you need to know if an object that WAS open is now closed when you can check for the existence of the object? I don't understand that.).
What am I missing here ?!?
I find it strange that I can grasp some concepts that other developers I work with don't know, but this eludes me. Unless the logic used actually IS flawed. ;)
Transistor
11-16-2002, 03:27 AM
Hey! guess what?
IE in Windows does not like that a JavaScript in a secure window (SSL) tell it's parent to reload, can you believe it? :eek:
All I had to do was change the URL, from https to http and both window.opener.location.reload() and window.opener.history.go(0) worked just fine.
Guess I'll have to change the login page to work differently.
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.