window.close() function in javascript - javascript

I have a problem with window.close() function. I have a simple HTML script:
<body>
gmail<br />
google<br />
<input type="submit" id="submit" name="submit" value="submit">
<script src="project.js"></script>
</body>
I want to open the above url's in different windows. Once the url's are loaded, the windows should close itself without any alerts. The below javascript code lets the url's open in different windows. But it closes only 'google.com' and the html webpage. The 'gmail' website remains open.
var submit = document.getElementById("submit");
submit.onclick = function() {
var getLinks = document.getElementsByClassName("one");
for(var i=0; i<=(getLinks.length-1); i++) {
window.open(getLinks[i]);
getLinks[i].onload = window.close();
}
}
What am I missing in this code. Any help would be appreciated.
Note: the links are only for test.

Check this out:
function openWin() {
myWindow = window.open("http://google.com", "myWindow", "width=200, height=100"); // Opens a new window
}
function closeWin() {
myWindow.close(); // Closes the new window
}
Easily found here:
http://www.w3schools.com/jsref/met_win_close.asp
Fiddle:
http://jsfiddle.net/zc2g7nkr/
It works fine in Chrome and i do not have to allow to block pop-ups.

It's not possible any longer to close a window you have not created.
Take a look at this window.close and self.close do not close the window in Chrome

The problem is that certain browsers do not allow you to close a window using javascript.
There are bugs that prevent it - specifically that the browser won't let code from one website close a window that is opened to a different website.
var win = window.open("http://www.google.com", '1366002941508','width=500,height=200,left=375,top=330');
setTimeout(function () { win.close();}, 3000);
Here is a DEMO
It works fine in Firefox when you allowing popup,
It opens a popup in chrome but it do not allow you to close a window using javascript.

A page to B page
window.open('B.html', '_blank');
window.open('','_self').close();
page close
window.open('','_self').close();

Related

How can i close an already opened window and then open a new window every time i click a button in angular?

I want to open a window using window.open(). but i have to make sure that if that same window has already opened previously , i have to first close that window then i have to open a new one .
try this code
<button onclick="openNewWindow()">Open New Window</button>
var popup_window;
function openNewWindow(){
if(popup_window != undefined){
popup_window.close();
}
popup_window = window.open("");
}
Try following code
let openedWindow;
let url = "www.example.com"
function openWindow() {
if(openedWindow) {
openedWindow.close();
}
openedWindow = window.open(url);
}
NOTE : The Window.close() 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.
Check MDN web docs

Close Tab Chrome/Safari Browser - Javascript

I am trying to close Chrome/Safari browser tab using javascript. I tried all the below code. But nothing is working.
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
Is it a browser restriction?
The window.close doesn't work because it doesn't know what to close try this
//Global var to store a reference to the opened window
var openedWindow;
function openWindow() {
openedWindow = window.open('moreinfo.htm');
}
function closeOpenedWindow() {
openedWindow.close();
}

Javascript popup window in background after userinteract

I want to open a little popup after clicking on a button.This Popup should open in the background.
This is my Code
document.getElementById('btn').addEventListener('click', function() {
var newWindow = $this.open('/popup',"MyTitle", "width=600,height=400,scrollbars=yes,resizable=yes,location=no");
newWindow.blur();
this.window.focus();
}, false);
What is my mistake? Why does it not open in the background?
Have you tried window.focus(); instead of this.window.focus(); ? I'm unsure if this will work as most browsers it causes security issues. Popunders aren't popular!

Window.open issue in firefox and IE

I am opening a new window on clicking a hyper link.
Issue:
After minimizing the window, again if I click on hyper link, the same window should be opened(In chrome minimized window will open up). But this is not happening in firefox and IE. Can anyone please help.
<html>
<head></head>
<body>
<p>Visit our HTML tutorial</p>
</body>
</html>
window.open allows you to specify a unique identifier to your popup; this allows you to open many links always in the same popup window.
If you use different identifiers on different links, it should open multiple popup windows.
<p>
Visit our HTML tutorial
</p>
<p>
Visit our HTML tutorial
</p>
If the strWindowFeatures parameter is used and no size features are defined, then the new window dimensions will be the same as the dimensions of the most recently rendered window.
you might want to check this link
window.open web api for mozilla
The idea of Unique ID in the parameter's list simply doesn't work as suggested in another answer.
You need a function for to do what you need in IE and FF. The trick is to get a function to see if it has opened a window before and do nothing if it has.
<script>
var opened = false;
function openWindow(){
if (!opened) {
w = window.open('', 'test', 'width=1500, height=900');
w.location = "http://www.google.com";;
w.onload = function() {
w.onunload = function() {
opened = false;
};
};
opened = true;
}
}
</script>
I'm using the opened global variable to track this. We set the newly created window to set false to this variable when it closes. Now the function can decide if it should really open a new window. Please note the following points:
We use onLoad function of the new window to set onUnload. Because IE seems to replace whatever the event handlers set here soon after it loads the page.
You can see that we first open a blank window and then set the url of it. This is because IE returns nothing when opening a new window if it is from another domain.

In Firefox, how do I bring an existing popup window with multiple tabs to the front using javascript from the parent window?

I would like to have a button on a web page with the following behavior:
On the first click, open a pop-up.
On later clicks, if the pop-up is still open, just bring it to the front. If not, re-open.
The below code generally works in Firefox, Safari, and IE8 (see here for Chrome woes). However, I have found a failure mode in Firefox that I don't know how to deal with:
If for some reason the user has opened a second tab in the pop-up window and that second tab has focus within that window, the popupWindow.focus() command fails to have any effect. (If the first tab has focus within that window, everything works just great.)
So, how can I focus the popup and the desired tab in Firefox?
<head>
<script type="text/javascript">
var popupWindow = null;
var doPopup = function () {
if (popupWindow && !popupWindow.closed) {
popupWindow.focus();
} else {
popupWindow = window.open("http://google.com", "_blank",
"width=200,height=200");
}
};
</script>
</head>
<body>
<button onclick="doPopup(); return false">
create a pop-up
</button>
</body>

Categories