English 中文(简体)
The proper way to handle popup closing
原标题:

I m looking for close event for popup. I ve found one for XUL, but I need it for HTML.

Popup has closed property.

>>> var popup = open( http://example.com/ ,  popup ,  height=400,width=500 );
>>> popup.closed
false

Well, I can check it once at half second.

function open_popup() {
  var popup = open( http://example.com/ ,  popup ,  height=450,width=450 );
  var timer = setInterval(function(){
    if (popup.closed) {
      alert( popup closed! );
      clearInterval(timer);
    }
  }, 500);
}

I ve tested it on Chrome 4.0.249.27, Opera 10.10, Safari 4.0.4, and Firefox 3.5.5. All works fine.

But setInterval bother me. It is ugly. Is there a better way of doing this?

UPDATE: I use popups for authentication dialog (oAuth, actually). I wanna send some data to parent window after popup close (through postMessage).

Page inside popup from another domain. So, I can not add any event (unload) to it due security restrictions.

I can not use iframe due to iframe buster script. So, I can not use any fancy jQuery modal dialogs.

I can not edit anything inside popup.

最佳回答

You might want to look into the unload event, take a look at Javascript: Popups

edit: as you ve said you cannot edit anything inside the popup, there really aren t any options left. I believe your current setInterval code does the job just fine. You should ask yourself if realtime detection of the popup closing is absolutely critical. That 500 milliseconds timer certainly won t strain hardly any resources or bring someones computer to its knees.

问题回答

I have used jQuery Dialog and it has a close event

http://jqueryui.com/demos/dialog/.

Am not sure if I understand your question right,why do you want to use the timer ?

Use window.opener in the pop-up window. i.e. something like:

onunload = opener.alert( popup closed );

or

onunload = opener.nameOfAFunction();




相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签