This question already has answers here:
window.close and self.close do not close the window in Chrome
(17 answers)
Closed 6 years ago.
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.
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()
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 (:
Is there any way to make window.close() work for "windows that were not opened by script using javascript" on Firefox?
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.
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.
Simply,
open(location, '_self').close();
The basic problem is that what your requirements-setter wants you to do is explicitly blocked by Firefox:
There is no way to hide the UI in a user's existing browser window, because it's user-hostile and a favorite tool of phishers.
There is no way to close the user's existing window from script, losing all the user's session history in the process.
Basically, the user has other things in the browser than just your bank thing. So assuming otherwise is more or less doomed to failure.
Is there any way you can push back on the unreasonable requirements?
This worked for me.
I used it to close the window when clicking a button in jQuery UI Dialog.
open(location, '_self').close();
Related
Before anyone marks this question as duplicate, I would like to say that all the other answers are from 2014 or older. The workarounds or hacks do NOT work anymore on the updated versions of Chrome.
I have a logout page that needs to be closed upon clicking a link. I have read about how Javascript cannot close the window if the same script hasn't opened the window.
I am currently using Javascript to achieve this. How can this be done? (Basically, shut the tab using Javascript).
I have tried the latest answers such as:
var win = window.open("window.opener.location", "_self");
win.close();
which don't work anymore! Any help is appreciated.
You can only close windows/tabs that you create yourself. That is, you cannot programmatically close a window/tab that the user creates.
For example, if you create a window with window.open() you can close it with window.close().
Is there an alternative way to close current tab using JS ? because window.close() is not working on Mobile browsers
I have the same issue, i tried a lot of ways, but none can adapt to all devices.
At last, I use the follow code, I really have no better way.
window.opener=null;
window.open('','_self');
window.close();
window.history.go(-1);
$(document.body).hide()
Some browsers will not allow you to close the window using window.close() unless the script opened the window. This is a little annoying sometimes. But there is a workaround to resolve this issue.
If you observe the error message that is thrown by Mozilla Firefox, Scripts may not close windows that were not opened by the script. it clearly says that if the script didn’t open the window, you can’t close that. But we open a blank page in the same window using “_self” as the target window and close the same window. In that way, the script opens the window (which is a blank one) and closes the window too.
So, to put this in the code:
<script>
function closeMe() {
var win = window.open("","_self"); /* url = "" or "about:blank"; target="_self" */
win.close();
}
</script>
and
<input type="button" name="CloseMe" value="Close Me" onclick="closeMe()" />
Ref: link
When I start Chrome, the new window appears as unfocused & behind the other windows. So I decided to create a workaround; I'll change my homepage url with a file from my disk which one contains javascript to steal focus and then redirect to real homepage.
What is the best method to steal focus, using javascript?
UPDATE: Here is what I attempted...
<html>
<head></head>
<body></body>
</html>
<script type="text/javascript">
myWindow=window.open("http://google.com", "_blank",
"resizable=yes, scrollbars=yes, titlebar=yes" );
myWindow.focus();
self.close();
</script>
However new window looks like popup window, Chrome doesn't display toolbars & adress bar is not editable :(
More info about the bug:
http://productforums.google.com/forum/#!topic/chrome/zyLC4Ip96pU
As far as I'm aware, this has only started on the latest update. If Chrome is already open, when I open a new window of Chrome from a shortcut in the quick launch area of my Windows toolbar, the new window does not take focus, the focus remains on the previous Chrome window. If the focus was not on the previous Chrome window when I launch the shortcut it works fine
I just published a small Chrome Extension which aims at solving this bug! So far it worked great for me (on Window 7 64-bit).
You can find it here: https://chrome.google.com/webstore/detail/chrome-new-window-focus/opmhcfhkhiodgapmnaolhmekmfihmecp
Let me know if you encounter any issues!
Cheers,
Yuvi
http://www.w3schools.com/jsref/met_win_focus.asp
So let say you have a page named daddy that opens a child window named testbug:
<html>
<head>
<title>daddy: Window.open bug</title>
</head>
<body>
<script type="text/javascript">
var new_win = window.open('', "testbug", 'toolbar=1, menubar=1, status=1, left=0, top=0', true);
</script>
</body>
</html>
If the user focuses on something other than the window with daddy tab and opens this page again the new daddy window opens up a new testbug child window. So now instead of having two daddy windows and one testbug window (as is the behaviour in IE && ff) there is two daddy windows and two testbug windows.
So my question is what do I need to keep this from happening?
PS: This is an internal application and is a very simple version of what we have on the real app.
UPDATE
I submitted this issue to google chrome team and will wait to see their response before deciding what to do with this question.
The only time I see a second testbug window being opened is if I close the daddy window and then reopen it. If I focus on another window and then refresh the daddy window, the same testbug window remains open.
Could you perhaps elaborate some more on which version of Chrome you are using, and your operating system? Your best bet might be to file a bug and work directly with the Chromium support team. They would be able to troubleshoot the scenario and determine if it is replicable.
We have written a small javascript function which checks if a URL should open in same window or a popup. In cases when a url should open in a new window IE is giving some strange behaviour a window popup flashes and closes with a beep sound. Can anybody suggest whats going behind the scenes i do not think its my javascript which is wrong. Is it some browser weird behaviour.
I suspect you have a third party pop-up blocker installed.
Yep, this sounds like some third party stuff. By default IE will try to guess what it should do with popups (open in a popup window, open in a new tab, don't open at all) but it won't try to open a window and close it immediately (unless caused by some addons or code included in the page). JavaScript errors shouldn't cause the window to close either (unless they really call window.close()).