Why is window.close() suddenly not working? - javascript

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

Related

How to exit Kiosk Mode in Edge programmatically

I'm developing a system that runs in Edge (school's default browser), the browser launches in Kiosk mode from a desktop shortcut
"C:\Program Files (x86)\Microsoft\Edge\Application\msedge.exe" --kiosk www.program-name.com --edge-kiosk-type=fullscreen
On the landing page, I can exit the program no problem with a close button using
<input type="button" onClick="window.close();" value="Close">
Once you proceed into the program, next page onwards, window.close no longer works. The only way to quit is by using Alt + F4 or pressing the Start key and right clicking the Edge icon
If you open the Developer Tools console, you'll find this message when the window doesn't close by window.close():
Scripts may close only the windows that were opened by them.
The message is clear. There're some restrictions when using window.close() to close window. In Chromium, window.close() succeeds if the new window/tab has an opener or if the back/forward stack contains fewer than two entries. The limitation of window.close() is for security and user-experience. For more detailed information, you can refer to this blog.
The only way to close the proceed window programmatically is opening the window by window.open(). For example, if you want to open a new window by clicking a link, you can use the code like below:
link
Then on test.html you can use window.close() to close the window.

Google Chrome closes when I open a new tab using my browser action Javascript, no idea why

I'm in the process of writing a Google Chrome extension, and I'm hung up on what should be a very simple task. I have a browser action that opens a popup, "popup.html", which then loads a small Javascript file, "popup.js". As soon as I get the DOMContentLoaded signal from the popup page, my Javascript opens a new tab with a certain URL. This behavioral quirk is by design - in some cases it will display a menu, and in other cases it will just open this URL.
I currently have some very simple code which is supposed to do this, but whenever it opens this tab, Chrome suddenly closes. I'm using a Mac (OS X Mavericks), so it doesn't crash entirely (still running), the window just closes. When I open it up again, the tab it was supposed to open is there, but the "popup.html" menu is awkwardly hanging over the UI still and won't go away.
Here is my complete popup.js file:
function stuff() {
window.open("http://stackoverflow.com/");
}
document.addEventListener('DOMContentLoaded', stuff);
I'm guessing that I may need to gracefully close my popup window before going to this link or something, but that seems like I'm overthinking it. After all, I've seen other extensions that have normal links in their browser action popups, and you can click those to open new tabs without any Javascript magic.
I've also tried using the chrome.tabs.create function, yet the same thing happens. The Chrome developer console doesn't show any errors. Thoughts?
Edit1: Just disabled all extensions and tried again. Still breaking. Going to boot up my Windows 8 box and see what happens on it.
Edit2: Works with a short delay before opening the window now on Mac, see my answer below.
Ok, I think I may have figured this out, but it's still weird. I did this on my Windows 8 PC, and the browser didn't crash, but the browser action popup didn't close the way it's supposed to either.
On a hunch, I altered my code on the Mac to give the browser some time to render the popup window:
function stuff() {
window.open("http://stackoverflow.com/");
}
setTimeout(stuff, 500);
Worked without a problem on the Mac this time. I'm guessing it's an issue with the browser being interrupted while rendering the popup window, but it's still weird. I would still appreciate an explanation from someone who actually knows what's going on here! :-)

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.

Close currently opened tab

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.

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