Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Html javascript to open new window and close current window

I have a popup window and in that page I have the following code in the body.

<a href="http://www.example.com" target="_blank" onClick="javascript:window.close()"><img src="...something"/></a>

The purpose is to have this popup window close when a user clicks on the image link, and to open a new page and be directed to http://www.example.com.

It works in IE and Chrome, but not in Firefox. The popup window closes but no new window is opened.

Any ideas?

like image 410
Eatdoku Avatar asked Sep 09 '10 22:09

Eatdoku


People also ask

How do I close the current window in HTML?

The Window. close() method closes the current window, or the window on which it was called. This method can only be called on windows that were opened by a script using the Window. open() method.

How do I close a new window in JavaScript?

JavaScript provides an in-built function named close() to close the browser window that is opened by using window.

How do you close window using JavaScript which is opened by the user with a URL?

The target attribute or name value of the window is given as _self. This is important as it makes the URL replace the current page. Step 2: Close this open window using the close() method: The window. close() method closes the window on which it is called.

How would you use JavaScript to open a new browser window?

In JavaScript, window. The window. open() method is used to open a new browser window or a new tab depending on the browser setting and the parameter values.


3 Answers

The question is actually solved for the opener but it didn't help my issue (not wished: the new windows under firefox keep the same size as the current popup).

So I find following solution:

function whenClicked() 
{
    window.close();
    opener.location.href = "http://www.example.com";

}

or this if the ppage should open in a new tab:

function whenClicked() 
{
    window.close();
    opener.open(http://www.example.com, '_blank');
}
like image 39
Samsky Avatar answered Oct 21 '22 23:10

Samsky


Yes, I can repro this - interesting. setTimeout works around it:

onClick="javascript: setTimeout(window.close, 10);"

I can only guess that once the window closes (which happens before the hyperlink is followed) Firefox stops processing that page.

Edit: better make it 10ms delay - with 1ms Chrome doesn't close the window.

like image 72
EMP Avatar answered Oct 21 '22 22:10

EMP


When you add some functionality to an element's click event via javascript, that functionality is executed before the default click event (in this case, opening a new page), in order to allow for the possibility of intercepting and overriding the default event. The default behavior will only execute when and if the event returns a boolean value of true.

In this case, the additional functionality would be to close the window and my guess is that Firefox chooses to interpret this as "we're all done here", so the click event never returns true, and thus the new page never gets opened.

Evgeny's suggestion of using a short timeout would allow the click event to return true before the window is closed, thus allowing the new window to open.

like image 42
Ender Avatar answered Oct 21 '22 21:10

Ender