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.
Related
I am somehow familiar with javascript and I am trying to learn all the properties and methods of window, document etc objects.My question is related with methods like resizeBy,resizeTo,moveTo,moveBy.I am seeing all the methods in action and all they do is effecting new window opened based on examples online.Is it possible that this methods effect the default window opened in a tab without trying to do in other windows opened?
According to https://developer.mozilla.org/en-US/docs/Web/API/Window:
In a tabbed browser, each tab is represented by its own Window object; the global window seen by JavaScript code running within a given tab always represents the tab in which the code is running.
So, yes.
I have Ie 11 to support. I open the popup using var myWindow = window.open('url','name','width=640,height=480,menubar=no,toolbar=no');
however, the handle returned (myWindow) is always null in ie.
I need to close that popup after certain events happen.
I know that it is possible because I've seen other sites do that in the same browser.
Any ideas?
If window.open() is returning null, then something is wrong. You're likely blocking popups, which means that the Window object will not be created (and thus myWindow will be null).
Check your security settings there and enable them.
Once you get window.open() to execute successfully, you can call myWindow.close() to close the popup when necessary. Note that the close() method can only close popups that have been opened using the window.open() method.
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
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);
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().