Javascript window.close() workarounds don't work on Chrome - javascript

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().

Related

Close Tab in Javascript

I am using angularjs.I have link suppose open tab now user right clicks on link and selects open link in new tab ,Now i have code in page abc.html like $window.close();.It is not working as expected i am getting Following Error Scripts may close only the windows that were opened by it.I googled on that i found only browser windows created using JavaScript can be closed using JavaScript.is there any way i can acheive my goal.
Thanks
If the user has chosen to open it in a new tab, you can't close it with JavaScript. However, if you use window.open(), you can close the opened window using JavaScript.
So the answer is no, unless you hack the user's computer.
It is possible.
try this:
window.top.close();
Working for me on all three major browsers.
if it does not work try :
open(location, '_self').close();

Chrome Opening Tab With Window.Open

Have looked at the answers to previous questions and am not sure if it
is the same issue as I am having. Some of the possible answers I have found
on the net are aged so am not sure whether anything has changed regards this.
I have this code to check for a cookie and then either open one or two
tabs dependent on whether a cookie is found.
On the html side I have a link like this:
<a target='_blank' onclick="newTab()" href="http://www.test.com">
In the head section the newTab() function does the cookie check then
if needed opens a window like this:
window.open('http://www.test.com/members/','_blank');
When the link is clicked the above works fine in Firefox and IE and both
windows open when needed but in Chrome only the onclick tab opens and not
the main url regardless of the cookie.
I imagine this is an issue relating to opening tabs/windows in Chrome but
I thought that the above would be dealt with by Chrome as the opening windows
are the result of a user click.
Am I missing something simple or is it a restriction on Chrome?
Thanks for any advice.
Indeed as answered by CBroe the reason for the problems was because
I had used _blank as the window name for both.
I changed one over to _parent and it is at least working like this now
so thanks for the assistance.

window.close() is working only in IE. How to close current tab in firefox window using javascript?

window.close() is working only in IE.
How to close current tab in Firefox window using javascript?
Pls help me
It's difficult to say wothout seeing your code, but I came to learn that often the cause of such behaviour could be that the window hasn't been opened by javascript so it doesn't allow it to close.
What you can do to go around this is to add a window.open just before your window.close statement; have you already tried that?

How to get window.focus() to work in firefox [duplicate]

This question already has answers here:
window.focus(), self.focus() not working in firefox
(3 answers)
Closed 9 years ago.
Does anyone have a clue on how to be able to use window.focus() in FireFox? (27.0.1).
I have tried all the about:config settings out there but still no luck.
My code looks something like this:
var wi = window.open('myUrl');
wi.focus();
== EDIT ==
I'm trying to make this work in FireFox on my machine, not for any other users so please
refrain from the standard "let your users decide" or "it depends on the user's settings" as
I want to get this to work for a local project :)
Tools > Options, Content area, "Advanced" button to the right of "Enable JavaScript" should give you the option to allow this behavior. If you're on Linux, the navigation might be slightly different.
Note, it is generally considered best practice to allow the user to choose what window should be in focus.
EDIT:
This is about:config you're referring to: dom.disable_window_flip
In FF 27.0.1 I am able to open a new window with focus by providing window name and option parameters to window.open, and then calling focus.
var wi = window.open('http://www.google.com', 'window_name', 'height=200,width=200');
wi.focus();
Example here: http://jsfiddle.net/CLVh2/
For Chrome support, you'll want to check out: Google Chrome "window.open" workaround?
EDIT: FOR TAB SUPPORT
If you want to open a tab and give the tab focus in FF (27.0.1), and simply calling window.open('url') is not giving you the desired result, try checking the "When I open a new tab, switch to it immediately" option. It can be found in Tools > Options > Tabs
According to Window.focus specification from Mozilla, that's not possible. It depends on the user's settings.
Makes a request to bring the window to the front. It may fail due to user settings and the window isn't guaranteed to be frontmost before this method returns.

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

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();

Categories

Resources