Dayzza
Dayzza

Reputation: 1557

open new windows in javascript

hi how to use javascriptscript to open a new window and then closing the current window..

currently, i can only open a new window, however my current window is still left open.

Code:

function newfunction()
        {
            window.open("index.html", "myWindow", "status = 1, height = 300, width = 300, resizable = 0");
        }   

Upvotes: 0

Views: 149

Answers (4)

Shadow Wizzard
Shadow Wizzard

Reputation: 66388

Here is the code you need:

function newfunction()
{
   window.open("index.html", "myWindow", "status = 1, height = 300, width = 300, resizable = 0");
   window.close();
}

This will work only if the current window was opened via script as well, otherwise IE show warning dialog and other browsers might simply ignore it.

Upvotes: 0

Shakti Singh
Shakti Singh

Reputation: 86446

add window.opener.close();

 function newfunction()
            {
                window.open("index.html", "myWindow", "status = 1, height = 300, width = 300, resizable = 0"); 
                window.opener.close();

            }   

or add this on the index.html file whenever a new window open it will close the parent window

<body onload="window.opener.close()">

Upvotes: 0

Florian M&#252;ller
Florian M&#252;ller

Reputation: 7795

use like you said window.open and then in the main window window.close(). But if you use like this, the browser will ask you "This website wants to close this window blabla".

Upvotes: 0

Jakob
Jakob

Reputation: 24370

Opening a new window and closing the old one sounds very much like changing the current page to me. Use location.href = 'newurl'.

And if you want to change its size and so on, that can also be done.

window.innerWidth = 300;
window.innerHeight = 300;
location.href = 'newurl';

Upvotes: 3

Related Questions