Close currently opened tab - javascript

I've tried window.close(); to close the currently opened tab but it does not seem to work. What am I doing wrong? My scope is to run a JavaScript script that automatically closes a manually opened tab.
Thanks.

Typically in javascript (or other flavors of javascript), this is done with...
Close Window
$(".close").click(function {
window.close();
});
Note that this will only work on the tab that contains the link that was clicked. It will not close other tabs open in your browser.
I'm giving a +1 for each comment on the question, too.

You can have a look at this answer how-to-close-current-tab-in-a-browser-window
Have a look also at the comments of this answer, where Ryan Joy says:
"This method is only allowed to be called for windows that were opened by a script using the window.open method." In other words, you can only use JavaScript to close a window/tab that was spawned via JavaScript"
Also notice that:
window.close();
works only for Internet Explorer where a popup message appears asking if user agrees for current tab/window to be closed.
In Firefox, Chrome only works for empty tabs and tabs/windows opened via "window.open()" method as mentioned above.

Related

Why is window.close() suddenly not working?

My application opens new PHP from an icon or link in a new browser tab. For several months now, I've been using a simple JavaScript function to close the current browser tab.
Suddenly, over the past few days, the Close link only works when the Window is first opened. The moment you do something on the page (click a link, press a submit button), it doesn't work. I'm at a loss as to what the problem can be. Nothing has changed in the code.
The HTML href statement is:
<span>Close</span>
And the JavaScript function is:
function windowClose() {
window.open('','_parent','');
window.close();
}
This is happening with both Edge and Chrome on a Windows 10 platform.
Could it be caused by some update to the O/S, Chrome or Edge application?
According to MDN, "scripts can not close windows, they had not opened"
If the tab has been opened by you (you typed the url and hit enter manually), there's no way you can close the window with Javascript. However, if for instance, you started your project with npm start (React in my case), the page can be closed with the code you've provided. Though when you try to re-do it by manually opening the webpage and closing the tab - you will fail.
All in all, don't try to close the window that was not opened by js.
Reference: https://developer.mozilla.org/en-US/docs/Web/API/Window/close#closing_the_current_window

How to close a window using Javascript?

I tried to close a window using window.close() when user clicks on cancel button, but window not closing.I am getting the following message in console
scripts may close only the windows that were opened by it.
this question has already been answered HERE
This method window.close() is only allowed to be called for windows that were opened by a script using the window.open() method.
It's a security to prevent a website taking control of your browser and closing windows.
You can try this. As it has been used here Reference Link
function CloseWithWindowOpenTrick()
{
var objWindow = window.open(location.href, "_self");
objWindow.close();
}

Best way to open/close browser window?

I have request from the customer to adjust some old functionality in the system. The current file has href links that look like this:
#Name#
If you look the code above you see that target="_blank" page will be opened in the new browser window. However, user wants to be able to close that window if they click OK/Cancel button in page.detail.cfm. I tried using this code for closing the browser window:
var closeBtn = document.getElementById('btn_cancel');
closeBtn.addEventListener('click', cancel);
function cancel(){
window.close();
}
After I tested the code and clicked Cancel message in the dev tools looks like this:
Scripts may not close windows that were not opened by script.
I guess that window can't be closed if not previously opened with JavaScript. I'm not sure what would be the best approach to solve this issue? Thanks for your help.
Technically a script is not allowed to close a page that a user has opened as opposed to it being opened by a script itself. It's a browser security issue. I know there were some hacks for it but thing like this get patched pretty quick from what I can tell. You could technically open the window with a script instead with some sort of click event or such, but again this is a bit of a work around. Check this out https://developer.mozilla.org/en-US/docs/Web/API/Window/close

Close Browser window while unloading

I have MyPage.aspx html page (generated using ASP.Net). When user tries to navigate away from this page, I need to close the window – user should not be able to go back or navigate to another page.
When I used window.close() inside window.onbeforeunload event, it asks for a confirmation to the user. “The webpage you are viewing is trying to close the window. Do you want to close the window?” On clicking “No” the user can escape the close attempt. Is there any way to forcefully close the window without giving an option to the user?
Reference:
How can I close a browser window without receiving the "Do you want to close this window" prompt?
Html javascript to open new window and close current window
"Unknown Exception" when cancelling page unload with "location.href"
Display confirmation popup with JavaScript upon clicking on a link
You can "trick" the browser like this:
window.onbeforeunload = function() {
window.open('', '_self', '');
window.close();
}
It seems to work in chrome/safari/ie/ff: http://jsbin.com/olijig/1
Firefox seems stubborn, but there might be another way to do the same in FF.
I should probably say that this technique is in no way standard and I don’t recommend it at all, and this code might break in many browsers besides firefox.
UPDATE
It actually works in Firefox too (latest version), but not older versions (I tried 3.6.1). You need to do some more testing to confirm the browser compatibility.
No, you can't. The user must be always capable of controlling whatever happens in his browser.
I'm not positive about this, but I believe if you have a window open another window, the parent window can close that child window. Would it be practical to have a landing page that opens your app in a separate window that could then close the window through javascript? Someone can probably elaborate more, as I haven't done this myself.

JavaScript close window for external URL

In my page when I click on a link a popup will be opened. In that popup, I have the close button. To close the window I am using simple JavaScript function as window.close().
This is working fine.
Now when I copy the URL of the popup link and open it in new window, I am not able to close the window.
In Firefox when using Firebug the warning given is:
'Scripts may not close windows that were not opened by script.'
Please help me out on any other alternative.
Firefox seems to answer that question: 'Scripts may not close windows that were not opened by script.'
This is a security measure. Imagine every site could close every other page you have open, that wouldn't work very well would it?
That's why only a parent window may close its children windows.
There may be a setting Firefox that allows windows not opened by script to be closed by script, but even if there is, what chance is there that your visitors will all have enabled this setting?
You can't work around this problem, it is how Firefox (and certainly other browsers) works. The only answer is to change your approach.
Why are you using windows as popups anyway? This has not been recommended for some time now and is mostly frowned upon. Popups that are actual windows may be blocked by popup-blockers.
You should probably use a modal popup instead of a window

Categories