2

I made a new window using window.open(); and I want to close it. So I made this code.

$('#open').on('click', function () {
    var win = window.open("", "", "width=400, height=200");
    $newWindow = $(win.document.body);
    // more code
    $newWindow.find('#close').on('click', function () {
        win.close(); // just works once
    });
});

And it works good in FF, works just once in Chrome (close button stops working), and does not work on IE11 (just tested v11)...

What am I doing wrong? ie, how to fix this to work cross browser?

jsFiddle

Rikard
  • 7,485
  • 11
  • 55
  • 92
  • Looks like there's an error logged in the console for IE11: "Exception thrown and not caught" – Igor Feb 17 '14 at 21:47

1 Answers1

1

The problem is with this line:

$newWindow.html(content);

You need to clone the element before you add it to the popup. Otherwise you are removing the original element and moving it to the new spot.

$newWindow.html(content.clone());

Updated Fiddle: http://jsfiddle.net/XL7LR/10/

gen_Eric
  • 223,194
  • 41
  • 299
  • 337
  • Aha, good point. That fixes Chrome. Any idea why IE still "doesn't work"? There I don't even see the `close` button – Rikard Feb 17 '14 at 21:42
  • Thank you for pointing out the main problem! Now I just need to know __[how to make it work on IE](http://stackoverflow.com/questions/21840660/reach-content-of-new-window-open)__ – Rikard Feb 17 '14 at 22:35