在开发用于销售的MVC应用程序中,我有一个按钮可以打开一个网页,该网页不允许iframe在新标签页中打开。因此,当销售代理使用此按钮时,他们通常不关闭应用程序打开的选项卡,到一天结束时,有20-30个打开的选项卡。因此,我想知道是否有可以添加到可以关闭的新按钮的脚本:

  • 包含所有选项卡的完整浏览器,使它们可以重新启动,也可以启动
  • 关闭所有其他选项卡,而不影响当前选项卡。

  • 认为,我有
    的HTML
    <input type="submit" onclick="return OpenInNewTab('http://test.com');" name="command" value="nonIframe" background-image:url(../images/URL/Test.png);" class="submit" />
    
    Java脚本
    //Function OpenInNewTab
    function OpenInNewTab(url) {
        var win = window.open(url, '_blank');
        win.focus();
        return false;
    }
    
    我正在使用此脚本,但是它只关闭当前选项卡。我希望打开当前选项卡,然后关闭所有其他选项卡,或者关闭整个浏览器本身。
    Java脚本
    <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/

    10-14 14:40
    查看更多