How to close current browse's tab? - javascript

I am trying to close the current open tab after 1 second.
Here is what I tried :
$(function(e){
setTimeout("window.top.close()", 1000)
)};
The code works in Edge but it does not work in Firefox.
I get this warning into the console when I try to close a page in Firefox :
Scripts may not close windows that were not opened by script.
How can I close the page?

Simply you can not. The fact Microsoft Edge allows you to close tabs is a serious security breach, other safer internet browsers will not let you do something like that.
You can close tabs and windows which were opened by the script itself.
Your code seems correct anyway.

Related

CLOSE | Chrome Tab | Javascript

Please help me with closing a Chrome Tab using Javascript.
I tried these - But all are failing.
open('about:blank', '_self').close(); - This opens up a new tab, and the user is able to browse back. Which is not required.
open(location, '_self').close(); - Scripts may close only the windows that were opened by it.
window.open('','_parent','');
window.close(); - Not working
As far as i know Chrome and Firefox have removed this feature of closing tab through JavaScript . Not sure about IE. Yet you can try this, it might work
(Remember you can only close those tabs which you yourself have opened through script)
Try
windows.top.close();
Hope this helps!

Firefox:4.0 window.onbeforeunload works when closing tab but not on closing the complete window

I am facing a wired scenario in Firefox 4. When I close the tab window.onbeforeunload is getting called. But when user is closing the window by the cross bar it is not getting called.
Is that a bug in Firefox or there is some other thing I need to do . Please check my code.
$(document).ready(function(){
window.onbeforeunload = function(){signout()};
});
window.onbeforeunload = function(){signout()};
Even writing this code outside document.ready is also not working. The same code is working on chrome.
The scenario get's even wired when their are more than one tabs are opened and then user closes the window (not the tab) by cross bar. In this scenario , Firefox asks the user to confirm that he is closing two tabs and after that my code works !!!. This is quite surprising my code does not work only when my page is the only tab and the user closes the window.
There's a thread on it here:
window.onunload only fires when a tab is closed in firefox, not the entire browser
It looks like it was a feature around that time to prevent unscrupulous websites from preventing browser being fully closed. I wouldn't be surprised if this is still the case, but the only way to find out is to try on a more current version of Firefox.

How to close the current tab of Internet explorer in vbscript or javascript

I want to close the current tab of Internet explorer on click of 'Log Out' button. It is a new functionality to be implemented in my web application. My application supports only Internet Explorer. The page on which Logout button is located is combination of many pages of window.close() is not working.
I have used following code in vbscript to close the current window of browser for Internet Explorer:
if(window.opener = Empty) then
dim x
x = "<%=Request.Browser.MajorVersion%>"
if(x="6") then
window.parent.opener = "x"
window.parent.close
else
'I need code for closing the tab here for IE7
end if
end if
I have used "<%=Request.Browser.MajorVersion%>" to fetch the version of Internet explorer. Itf it is IE6 then code works fine because there is no tab functionality in IE6 but how to close the current tab for IE7. Please help me out in this context so that I may close the current tab for IE7 also. Thnaks in advance!
You can try this to close the tab in IE7:-
window.parent.opener = "x"
window.parent.close
I have faced the same issue and found that this was working fine for me. Although this would give you an extra message(Microsoft warning message):-
Modern day browsers will not allow you to close a window or tab that you have not created with window.open. Any ways of doing it to make it work is a HACK and is bound to break when browsers are upgraded.

Strange Window popup behaviour on IE7

We have written a small javascript function which checks if a URL should open in same window or a popup. In cases when a url should open in a new window IE is giving some strange behaviour a window popup flashes and closes with a beep sound. Can anybody suggest whats going behind the scenes i do not think its my javascript which is wrong. Is it some browser weird behaviour.
I suspect you have a third party pop-up blocker installed.
Yep, this sounds like some third party stuff. By default IE will try to guess what it should do with popups (open in a popup window, open in a new tab, don't open at all) but it won't try to open a window and close it immediately (unless caused by some addons or code included in the page). JavaScript errors shouldn't cause the window to close either (unless they really call window.close()).

Strange behaviour opening pop-up window in Internet Explorer

I have the following JavaScript code to pop up a window in Internet Explorer. The code is executed from a page within a Microsoft CRM modal dialog box. (RequestID is a string that is always the same in the test system - at the moment it contains "abcdefg" - without the quotes).
var cancelUrl = "CancelRequest.aspx?RequestID=" + RequestID;
alert("About to open a window.\n\n" + cancelUrl);
window.open(cancelUrl);
alert("Window opened");
I expect to see a message telling me that I am about to open a window. I then expect to see a window open and get another message about the window having been opened. I don't really care about the order of the last two events; the alerts are there so I know the code has been executed.
I have two PCs and a virtual PC. All running IE7. On the Windows 2003 VPC, the messages and pop-up appear every time without fail.
On the Vista PC and WinXP PC, the messages appear but the pop-up only appears intermittently. (I think this may be the case on the Vista PC too).
All three have identical settings in IE. All have the IE pop-up blocker disabled and have no other pop-up blockers installed.
Can anyone shed any light on this?
Ah, I think I got it... missed it in the description...
You are trying to open a non-modal window from a modal dialog in IE.
This AFAIK, should not work.
Try opening another modal window instead.
Effectively you are saying...
on window A, open up modal window B, now open up non-modal window C, which isn't really valid.
This code is simple. Use debugger and see what is going on.
Check that site with FireFox or Chrome, they have JS debuggers.
Edit:
Add try/catch block around window.open() and see if there is some exception there.
Edit 2:
I see now that you are sending characters as RequestId. You should check if that URL can handle that kind of value. Since name is RequestId I'd say that there is big chance that there should be numeric only parameter. If that is correct, then it can happen that server side crashes when you try to open window and then nothing happens. Reason more to set try/catch block and test.
You might want to try Firebug lite, which will work for IE.
http://getfirebug.com/lite.html
The try/catch other people have mentioned is also a good idea. I think.
Additionally, is there any chance that the pop-up is trying to use a window that is already open but minimized. So it doesn't appear to be working but it's really just reloading the minimized window?

Categories

Resources