1

I'm opening a new window and assigning it an onload event like this:

var wconsole = window.open("console?cachebust=" + (new Date()).getTime(), "consolewin");
$(wconsole).load(function(){
  // event code here
});

However, if I make the same call again (i.e. call window.open again with the same arguments), the load function is never called, even though the window is refreshed.

Any ideas why?

Ovesh
  • 5,209
  • 11
  • 53
  • 73
  • Not sure if just missed it in the copy/paste but you're missing a closing paren after the closing brace on the last line. – sachleen Jul 27 '12 at 04:24
  • If the window is already open, what do you expect it's load function to do? It's already loaded. Close the window and it **loads** the window again. – sachleen Jul 27 '12 at 04:25
  • What do you mean by "make the the same call again"? Do you just add a new `load` listener to the window, or do you open a second window and add a `load` listener to that one also? – apsillers Jul 27 '12 at 04:44
  • possible duplicate: [Detecting the onload event of a window opened with window.open](http://stackoverflow.com/questions/3030859/detecting-the-onload-event-of-a-window-opened-with-window-open) – Alexander Jul 27 '12 at 04:51
  • @sachleen : What I mean is, when I call `window.open` again, you'll note the URL is different (thanks to the cachebust), and in fact the server generates a different page. So the page is refreshed, in other words it "reloads". Just as you would expect a refresh on the original page to cause onload to fire, I expect the same here. – Ovesh Jul 30 '12 at 01:33
  • @sachleen : I guess that's a good solution, actually. Close the window and reopen it. Why didn't I think of that? If you post it as an answer I'll give you the score. – Ovesh Jul 30 '12 at 01:38

4 Answers4

0

Try this. not sure but worth a try.

var wconsole =NULL;
wconsole =  window.open("console?cachebust=" + (new Date()).getTime(), "consolewin");

$(wconsole).load(function(){
  // event code here
}
Imdad
  • 5,942
  • 4
  • 33
  • 53
  • Are you sure upper-case NULL is supported in JS? I don't see how this will be any different. – Ovesh Jul 30 '12 at 01:34
0

You were missing a closing brace, maybe that's the issue.

var wconsole = window.open("console?cachebust=" + (new Date()).getTime(), "consolewin");
$(wconsole).load(function(){
    alert('test');
});
Collins
  • 231
  • 2
  • 14
0

As I stated in my comment, if the window is already open, what do you expect it's load function to do? It's already loaded. Close the window and it loads the window again.

You can use the window.close function to close the window and then open it again.

wconsole.close()
sachleen
  • 30,730
  • 8
  • 78
  • 73
  • The solution is good. But it still remains that it should behave differently. The window may already be open, but if the document is reloaded, then onload should fire. – Ovesh Jul 30 '12 at 03:38
  • I think it might be because there's a difference between the window and document. You only have one window, but you reload the document inside of it. – sachleen Jul 30 '12 at 04:37
-1

Are you sure you don't mean $(wconsole).onload(function(){...}

In any case failure is guaranteed since a reload smashes everything, including any scripts (but perhaps if Grease Monkey has persistent scripts) and so any functions that were defined become redefined. Unless the reload redefines, anew, the functions that are needed, the game is lost.

Example:

javascript:
void(window.open("data:text/html,
<html>
<a href=\"javascript:alert('Here today ...');location=location.href;alert('gone ...');\">
reload </a> shows only 1 alert <br/>
(2nd may appear but disappears automatically once window has refreshed)<br/><br/>
as goes `alert` so do other `window` functions like `onload`<br/><br/><br/><br/>
<a href=\"javascript:alert('Now you see ...');self.close();alert('NOT!?');\">
choked or croaked?</a> only 1 alert is seen<br/>
</html>"))

Testing with:

 window.navigator.userAgent =  
 Mozilla/5.0 (X11; Ubuntu; Linux i686; rv:11.0) Gecko/20100101 Firefox/11.0

ref:
Open a new browser window on close of current window if user confirms to close current window

Bookmark:
window.onload not firing when window is already open

Community
  • 1
  • 1
ekim
  • 118
  • 1
  • 2