Automatically close a browser window when the user clicks outside it - javascript

Is it possible to automatically close a web browser window (e. g., in Google Chrome) when the user clicks outside the window? In order to do this, there would need to be some method for detecting whether or not the window was focused, and also a method for closing the window.

Most current browsers only let you close the child windows of a parent.
You can't close the master parent window through a script.

By default most browsers do not allow javascript to close windows that were not opened by javascript itself. But if you use e.g. window.open(...) you can close that window with window.close().
So if you have a window opened by javascript you can do the following:
window.addEventListener('blur', function(){window.close();}, false);

Related

Close browser tab (window) in angular

I there any trick that I can use to close browser tab (window) in angular. most of methods I tried are not working.
the error I got is "scripts may close only the windows that were opened by them". help to ignore this
The Window interface represents a window containing a DOM document; the document property points to the DOM document loaded in that window.
A global variable, window, representing the window in which the script is running, is exposed to JavaScript code.
Where the window.parent property returns the immediate parent of the current window, window.top returns the topmost window in the hierarchy of window objects.
You can simply use the following code:
window.top.close();
The Window self() property is used for returning the current window. It is generally used for comparison purposes while using other functions such as top().
You can simply use the following code:
window.self.close();
Warning!
Sometimes you can't close the current window in Firefox because you didn't open it.
Most people have their browser set to open new windows in a new tab, and you can't prevent the menu bar etc in a tab window.
If you use close() method, This method can only be called on windows that were opened by a script using the Window.open() method. If the window was not opened by a script, an error similar to this one appears in the console:
Scripts may not close windows that were not opened by script.

How to check if non-chrome window is focused Chrome Extensions API

I'm working on a chrome extension that tracks the active chrome tab and chrome window, but I want it to know when the active window is a non-chrome window.
When using a listener for chrome.windows.onFocusChanged, the listener only triggers when the new focused window is a chrome window or if all chrome windows are minimized. It does not trigger if you maximize or alt-tab into another non-chrome window. Is there a solution for this or is this problem out of scope for the Chrome Extensions API?
This can be achieved (partially) with the Page Visibility API. document.visibilityState will return the value of hidden when the current tab is covered by another maximized program window, or when the tab is not visible.
You can add this to your content scripts:
var string = document.visibilityState;
Or as an event listener:
document.addEventListener("visibilitychange", function() {
console.log( document.visibilityState );
// Modify behavior...
})
Unfortunately, document.visibilityState is not accurate for all programs I've tested (such as Steam or Windows Settings), and it will only return the value of hidden when the chrome tab is completely covered from view, not partially.

Close multiple firefox windows from javascript

I have a GWT application which has many firefox windows. I need functionality to close all these windows with a single click on a button. I know the window.close() function, but it only closes the current window.
I wonder if there is a firefox plugin or anything what can help me achieve this functionality.
In Firefox the File menu contains an Exit button what terminates the program, but i can't find a way to call this from javascript.
You cannot close other windows with script that didn't open them. But
var newWindow = window.open( url );
newWindow.close();
will open new tab with url and then you will be able to close it with newWindow.close();
When a new window is opened with window.open(), a reference to the new window is returned.
This means var window1 = window.open("example.com", "windowName"); will allow you to close the this new window later with window1.close();
Windows are often opened this way to address your problem, though if the windowName specified existed already, the existing window would be used to load the page instead, and the variable would refer to the existing window.
EDIT: This explains browser windows and associated window control JavaScript here

How to open a new window with keeping mouse focus on the older window

I want to open a new windoe on clicking of a image and at the same time keeping my mouse cursor on the current window.
I tried to open a new window using target="_blank" but it brings my cursor over new window
Is there to any way to implement it?
It's what you call a "pop-under" (instead of pop-up)
The main idea is to build the pop-up using window.open and intentionally lose it's focus using blur()
however, you can't guarantee that it pops under or even over the browser. some browsers have extreme anti pop-up settings, others not so. others open in tabs instead of pop-ups.

Knowing whether window.close() will show a security warning

In IE7, a child window opened with window.open can close itself using window.close(), but a window opened with <a href=... target=_blank> will show a security warning if the child window tries to close itself.
In my application, I don't know how my child window is opened, and I need to know (in the child window JavaScript code) whether I can use the window.close() or not. Is there a way? Another way to ask the question is - is there a way in IE to differentiate between a window opened via window.open vs a window opened via target=_blank.
I tried checking window.opener but in both cases, there is a value there, so this does not allow me to differentiate between the two cases.
Try comparing window.opener and window.self
Source: Close window without the prompt message in IE7
This is how to avoid the prompt according to the page above:
function WinClose(){
window.open('','_self','');
window.close();
}
Close
Is this a possible approach for your page?
Just a blind shot, but you can try removing the onunload event in the window, in case it is there.
If you have control over the window.open events, you could give the new window a name (2nd parameter I think). You can then check for that name before applying window.close().

Categories

Resources