Close Tab Chrome/Safari Browser - Javascript - 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();
}

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

How do I close the current browser tab on ReactJS?

Basically, I want the current browser tab to be closed on a click of a button. How do I implement this in ReactJS? Tried window.close() but did not work.
For security purposes, JavaScript can't close a window that it didn't directly open.
https://developer.mozilla.org/en-US/docs/Web/API/Window/close
As you can see from the example, the originating script (which opened the window) must also be the script that closes the window. A new window isn't capable of closing itself via JavaScript.
//Global var to store a reference to the opened window
var openedWindow;
function openWindow() {
openedWindow = window.open('moreinfo.htm');
}
function closeOpenedWindow() {
openedWindow.close();
}
On your click event handler, try this :
window.open("", "_self");
window.close();

window.close() function in 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();

How do I close a window in JavaScript/jQuery?

I want to create a link that says 'Close' and it should close the window. Anyone have any idea how to do this?
I've looked at other people's questions and it seems I can't find anything that works.
This is what I have right now:
<a id="noThanks" href="JavaScript:window.close()">No, thanks</a>
There is javascript command for closing current window.
window.close();
or if you are opening named window
function openWin() {
myWindow = window.open("http://www.domain.com", "_blank", "width=200, height=100");
}
function closeWin() {
myWindow.close();
}
Using win.close() will close the window if win is a variable that has the value of a window.open() call stored in it.
So,
var win = window.open("//stackoverflow.com");
win.close();
will close the window opened by window.open().
However, window.close() only works on a window that was opened by JavaScript. That is, you can't close a window that a user navigated to by clicking a link.
To control a window, you'd have this:
Open Window
Then, in your new window, you'd have this to close it:
<button onclick="win.close();">Close</button>
You can only close a tab/window that you've opened using window.open
Similar question and answers can be found here
This is the answer I was really looking for in case anyone else ever needs it.
Just a heads up--this doesn't work in FF.
window.open('', '_self');
window.close();
In my case once data saved I need close window after confirmation .
<script type="text/javascript">
if (confirm("Close Window?"))
{
close();
}
</script>
Don't forgot to call jquery library before your script

unable to close tab after log-out in firefox

i tired to find out way to close tab after log out in Firefox.
<script type='text/javascript'>
window.open('','_self');
window.close();
document.execCommand('ClearAuthenticationCache');
</script>
working in other browser but problem in Firefox. i need to close tab when signout/logout attempt.
Firefox will not allow you to close windows or tabs that a user opened.
Most financial sites for this reason will ask users to click a button and open a new window.
you can do this with window.open. Store the handle returned by that and use it to close the window you opened. The user may have many other tabs open in that window.
Refer: window.close
This is how you close a window that you open.
var openedWindow;
function openWindow()
{
openedWindow = window.open('moreinfo.htm');
}
function closeOpenedWindow()
{
openedWindow.close();
}

Categories