How to close a window using Javascript? - 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();
}

Related

How can I open a pop-up window without it being blocked?

I haven't found a single answer able to tell me what's the right way to open a popup.
Times have changed, and popups have been mostly replaced with fancybox-like boxes. However, there are still times when popups are needed.
For those cases, I don't want my popup to be blocked by the browsers.
What's the right way to open a popup without it being blocked? Even if it opens a new tab in the browser. I just want my popup to be open, and have control of it from the parent or vice versa.
Popup blockers will block any popup, unless it is opened because of an user action.
If the user clicks on a link, and a popup is opened in the click listener of that link, the popup blocker knows the user want to open something and will not (or should not) block the popup.
What you cannot do:
open a popup when the page is opened or closed
open a popup after a certain interval
open a popup after something asynchronous happens
What you can do:
open a popup in the on click listener
using target="_blank" in a anchor tag
You can access both windows with JavaScript variables:
if you use window.open, the parent can have a reference to the popup by assigning the result of window.open to a variable. Check out this article at W3Schools.
If the popup needs to have access to the window who has opened it, you can use window.opener. Check out this question.
try this, it works for me
$('#myButton').click(function () {
var redirectWindow = window.open('http://google.com', '_blank');
redirectWindow.location;
});
Js fiddle for this is here https://jsfiddle.net/safeeronline/70kdacL4/2/
if you want to open new tab after ajax call see this fiddle http://jsfiddle.net/safeeronline/70kdacL4/1/

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 tab after window.open()

I've got a page which open a new window fullscreen but it still has the other page (the one which re-directs) behind so when you close the new window you are taken back to a blank page.
Is there a way to open a new window and then close the tab which is now inactive?
Currently I have some javascript like this
window.open("http://website.co.uk");
Thanks
You can technically use window.close() on the opening tab, but browsers usually will not allow JavaScript to close windows that it has not opened. Can you redirect to some useful page that the user can use once they have closed the window?
If you can add some JavaScript code to the page being opened, I think you can use window.opener.close() to close the original window. I tested it in IE8, the browser will ask for confirm before close the original window.

How to close a window without confirm dialog?

window.close();
The above will pop up a confirm dialog each time, and not working at all in Firefox.
Use this way
window.open('','_self');
window.close();
I think that's not without a reason. People don't like windows being closed without notice.
Closing a browser window is not as straight-forward as it used to be years ago.
Typically, a newly opened window can be closed if:
1. the 'close' is called within the DOM of the window itself
2. the closer is the opener
However, with almost all browsers having tabs, if the opened window is the only remaining tab in the main window, it might not close without prompt for above case 1. Even if it closes, it might just close the tab and leave the main window opened.
You can't - it's a security feature. You'll need to look into showing some form of modal dialog if you wish to be able to close it. Have a look at something like This JQuery Example which features auto-close
Windows not opened by JavaScript cannot be automatically closed with JavaScript (and I can't think of any good reason for a website to close the window that the visitor arrived at the site with, discarding their Back history in the process)
it works in chrome,firefox need to turn the "allowjavascriptclosewindow" option on.
ie need alter your code as:
window.open("","_self");
window.close();
The below javascript works fine to close the tab with user confirmation.
<script>
function closeWindow()
{
if (confirm('Are you sure you want to close the Window? All the unsaved data will be lost')) {
top.window.open('','_self','');
top.window.close();
}
}
</script>

Categories