I'm trying to write a simple javascript(no jquery or any other framework) which should automatically close the browser window or tab (if it's open in a tab). I found a lot of examples over the internet but I couldn't find a reliable solution( some of them don't work with IE) . If it's not possible to close the window directly without the client action a simple link to click on would be good too . So far I have this
Close Window
The code you posted will work fine, except you can only close windows that were opened by your script (by window.open). There's no way you can close a window/tab that the user manually opened.
Related
I am working on a web application where I am using JavaScript for the client side scripting. Now my requirement is to close all the opened window which were opened through window.showModalDialog().
For this, I read the history of the browser using window.history.length, but I do not know how to close each window. This works well for window.open(), but not for window.showModalDialog().
Could you please guide me to move forward?
You can close the opened window as follows:
To Open:
var window1=window.open("http://somedomain.com");
var window2=window.open("http://someotherdomain.com");
To Close
window1.close();
window2.close();
But be sure you call window1.close() and so on.. on the same script where you opened it.
You should not be using window.showModalDialog. Firefox has deprecated it, and Chrome has removed it. Also take a look at window.showModalDialog: What It is and Why You Should Never Use It.
The idea of showModalDialog is that all scripts are paused while the modal window is open. Thus, using window.close doesn't work, since as long as the window is open, no more scripts are being executed.
One possibility is to have JavaScript in the modal dialog so that is closes itself. You will not be able to close it from outside.
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.
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.
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
I have an application, in that I want to close the entire browser on clicking on the close button from our application. I tried with the window.close() and self.close() functions but they will be closing the browser if the window is opened from window.open().
Please suggest to me how I can close the browser on clicking on the close button?
You can't. window.close() used to do that in a far past, but browsers don't allow that anymore, going from the concept of that a website should be able to stay open as long as it has to. Eg. a website doesn't end. If you log out, you don't close the window, you simply go back to the home page where you let the user log in again. Same with smartphone apps btw :-)
Most modern browsers will only let you close child windows of a parent. You cannot close the parent window through script.