在我为销售而开发的 MVC 应用程序中,我有一个按钮可以打开一个不允许 iframe 在新选项卡中打开的网页。因此,当销售代理使用此按钮时,他们通常不会关闭应用程序打开的选项卡,而在一天结束时,有 20-30 个打开的选项卡。所以,我想知道是否有一个脚本可以添加到一个可以关闭的新按钮中:
- 包含所有选项卡的完整浏览器,以便他们可以重新开始,或者
- 关闭所有其他标签而不影响当前标签。
在 View 中,我有
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;
}
我正在玩这个脚本,但它只关闭当前选项卡。我希望打开当前选项卡并关闭所有其他选项卡或关闭整个浏览器本身。
Javascript
<script language="JavaScript">
function closeIt() {
close();
}
</script>
HTML
<center>
<form>
<input type=button value="Close Window" onClick="closeIt()">
</form>
</center>
如有任何建议,我们将不胜感激。 谢谢你
最佳答案
只有掌握了句柄才能关闭窗口。这意味着您的页面必须是打开它们的页面(或者您以某种方式传递了句柄)。
例子:
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();
您可以将这些新窗口存储在一个数组中,并在需要关闭它们时遍历句柄。
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()
}
关于javascript - 关闭其他选项卡或浏览器的脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29220735/