Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Close windows that were not opened by script using javascript [duplicate]

I want my web application to run in a window which has no menubar, addressbar etc. I have a Default.html page and in this page I click a "Run Application" button which first opens my application in another window using window.open and then closes the current window by opening "AutoClose.html" using window.open with _self parameter

Default.html

<html>
<head>
<script type="text/javascript">
    function runApp() {
        // Open my application
        window.open('Application.html', null, 'status:no;dialogHide:true;help:no;scroll:yes;center=yes;');

        // Close this window
        window.open('AutoClose.html', '_self');
    }
</script>
</head>
<body>
    <input type="button" value="Run Application" onclick="runApp();" />
</body>
</html>

AutoClose.html

<html>
<head>
<script type="text/javascript">
    window.close();
</script>
</head>
<body></body>
</html>

My application should support IE, Firefox and Chrome and my code works fine on IE and Chrome but it is unable to close the first window on Firefox because of "Scripts may not close windows that were not opened by script" warning. On Firefox it opens the AutoClose.html but window.close() call in this page just causes the "Scripts may not close windows that were not opened by script" warning and window is not closed. By the way my application window is opened without any issues (no problem about that).

It seems that using window.open() with _self parameter trick does not work for Firefox. Since my goal is to run the application in a window without menubar, addressbar etc.

  1. Is there any way for hiding addressbar, menubar without using window.open() (at least for Firefox)? Then I will not need to run window.close()
  2. Is there any setting to suppress "Scripts may not close windows that were not opened by script" warning on Firefox? An "Allow scripts to close windows that were not opened by script" setting would be great (:
  3. Is there any way to make window.close() work for "windows that were not opened by script using javascript" on Firefox?
  4. Or, is there any other suggestions (:

Thanks in advance.

UPDATE: This is a banking application and I am just a developer not the decision maker. In other words some kind of analyst wants the application work in this way. And I am not questioning it. So "the whole thing you are trying to do is completely wrong" answers will not be really helpful.

like image 967
Mehmet Ataş Avatar asked Sep 07 '12 08:09

Mehmet Ataş


People also ask

How do I close a window that was not opened in a script?

close() method closes the window on which it is called. The window that was opened in the first step is closed by using this method. This works because the window has now been opened by our script instead of the user. Note: This approach may not work on all browsers due to different implementations of browser security.

How do you solve Scripts may close only the windows that were opened by them?

"Scripts may close only the windows that were opened by it." If your script did not initiate opening the window (with something like window. open), then the script in that window is not allowed to close it. Its a security to prevent a website taking control of your browser and closing windows.

How do I close a window using JavaScript?

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.

Can a window close itself JavaScript?

close() or self. close(). Since the child window was opened with script, window. open(); it can close itself.


2 Answers

Simply,

open(location, '_self').close();
like image 157
patrick Avatar answered Sep 20 '22 15:09

patrick


You can't close the current window in firefox because you didn't open it. It doesn't matter that you loaded AutoClose.html into it.

But this whole business with windows is pointless. Most people have their browser set to open new windows in a new tab, and you can't prevent the menubar etc in a tab window.

I, personally, would be very irritated if you closed my window just because I used your application.

It could be the only window the user has open - in which case you just closed their browser. It could be they want to press back, and closing the tab will annoy them.

This error from firefox is correct, and if it works in Chrome that's a serious bug and you should report it.

like image 23
Ariel Avatar answered Sep 18 '22 15:09

Ariel