Rikard
Rikard

Reputation: 7805

window.open & close several times

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

Upvotes: 2

Views: 97

Answers (1)

gen_Eric
gen_Eric

Reputation: 227240

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/

Upvotes: 1

Related Questions