Reputation: 285
In my MVC application being developed for sales, I have a button which opens a web page which doesn't allow iframe to open in a new tab. So, when the sales agent use this button, they often don't close the tabs opened by the application and in the end of the day, there is 20-30 of open tabs. So, I was wondering if there is a script which I can add to a new button which could close:
In the view, I have
<input type="submit" onclick="return OpenInNewTab('http://test.com');" name="command" value="nonIframe" background-image:url(../images/URL/Test.png);" class="submit" />
//Function OpenInNewTab
function OpenInNewTab(url) {
var win = window.open(url, '_blank');
win.focus();
return false;
}
I was playing with this script, but it only closes the current tab. I want the current tab to be open and close all other tabs or close the entire browser itself.
<script language="JavaScript">
function closeIt() {
close();
}
</script>
<center>
<form>
<input type=button value="Close Window" onClick="closeIt()">
</form>
</center>
Any suggestions would be really appreciated. Thank You
Upvotes: 19
Views: 50367
Reputation: 1388
working prototype based on @Gray's answer:
<html lang="en">
<head>
<title>Sample-close specific tabs</title>
</head>
<body style="text-align: center;">
<a href="#" onClick="openGoogle();">Google</a> |
<a href="#" onClick="openBing();">Bing</a> |
<a href="#" onClick="openYahoo();">Yahoo</a><br />
<a href="#" onClick="closeAll()">closeAll</a><br />
</body>
<script>
var winGoogle;
var winBing;
var winYahoo;
var windows = [];
function openGoogle(){
winGoogle = window.open('https://google.com', '_blank');
windows.push(winGoogle);
}
function openBing(){
winBing = window.open('https://bing.com', '_blank');
windows.push(winBing);
}
function openYahoo(){
winYahoo = window.open('https://yahoo.com', '_blank');
windows.push(winYahoo);
}
function closeAll(){
console.log("length: " + windows.length);
for(var i = 0; i < windows.length; i++){
windows[i].close()
console.log(windows[0]);
}
}
</script>
</html>
Upvotes: 0
Reputation: 3364
Building on Mohamed Musthaque's excellent answer ... I wanted to open a new tab and close the current one. This does it perfectly in Chrome
<a href='https:...'
onclick='window.open(this.href); window.open("", "_self", "").close(); return false;'>
Open new tab, close this one
</a>
Upvotes: 0
Reputation: 325
HTML:
<a href="javascript: window.open('', '_self', '').close();">Close Tab</a>
javascript:
window.open('', '_self', '').close();
IT will close the current tab without prompting a permission.
Upvotes: 3
Reputation: 7140
You can only close windows if you have their handle. That means your page needs to have been the one that opened them (or you somehow passed the handle around).
Example:
var winGoogle = window.open('http://google.com', '_blank');
var winBing = window.open('http://bing.com', '_blank');
var winYahoo = window.open('http://yahoo.com', '_blank');
//close the windows
winGoogle.close();
winBing.close();
winYahoo.close();
You could store these new windows in an array, and iterate over the handles when it is time to close them.
var windows = [];
//each time you open a new window, simply add it like this:
windows.push(window.open('http://google.com', '_blank'));
//then you can iterate over them and close them all like this:
for(var i = 0; i < windows.length; i++){
windows[i].close()
}
Upvotes: 27