Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
192 views
in Technique[技术] by (71.8m points)

javascript - Script to close other tabs or browser

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:

  1. Either the complete browser with all tabs in it so they can start fresh or
  2. Close all other tabs without affecting the current tab.

In the view, I have

HTML

<input type="submit" onclick="return OpenInNewTab('http://test.com');" name="command" value="nonIframe" background-image:url(../images/URL/Test.png);" class="submit" />

Javascript

//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.

Javascript

<script language="JavaScript">
    function closeIt() {
        close();
    }
</script>

HTML

<center>
    <form>
        <input type=button value="Close Window" onClick="closeIt()">
    </form>
</center>

Any suggestions would be really appreciated. Thank You

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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()
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...