【问题标题】:close .aspx using JavaScript/ Jquery使用 JavaScript/Jquery 关闭 .aspx
【发布时间】:2015-01-27 09:22:28
【问题描述】:

我已在其他链接(如
window.close and self.close do not close the window in Chrome 等)上搜索过此问题,但无法解决
我想使用以下代码关闭页面,但它不适用于任何浏览器。

我的网址是这样的:

http://server/solutionName/Confirmation.aspx

警告

脚本只能关闭它打开的窗口。

HTML

 <img alt="Close" src="images/close.png" onclick="return confirm_delete();">

脚本

  function confirm_delete()
   {
      if (confirm("Are you sure you want to close?") == true)
          window.close();

      else
          return false;
    } 


这是屏幕截图:

【问题讨论】:

  • 这段代码有什么问题?
  • 我认为(我可能错了,因为事情每天都在变化)在某些浏览器中,您无法使用 JavaScript 关闭窗口,除非它是使用 JavaScript 打开的
  • 你是怎么打开Confirmation.aspx的。
  • @Șhȇkhaṝ:通过向导。在下一个按钮上单击。这是向导的最后一页

标签: javascript jquery asp.net vb.net


【解决方案1】:

试试下面几行代码

        var Browser = navigator.appName;
        var indexB = Browser.indexOf('Explorer');
        if (indexB > 0) {

            var indexV = navigator.userAgent.indexOf('MSIE') + 5;
            var Version = navigator.userAgent.substring(indexV, indexV + 1);

            if (Version >= 7) {
                window.open('', '_self', '');
                window.close();
            }
            else if (Version == 6) {
                window.opener = null;
                window.close();
            }
            else {
                window.opener = '';
                window.close();
            }
        }
        else {
            window.close();
        }

【讨论】:

    【解决方案2】:

    window.close调用该方法时,被引用的窗口关闭。

    这个方法只允许被打开的窗口调用 通过使用 window.open() 方法的脚本。如果没有窗户 通过脚本打开,JavaScript 中出现以下错误 控制台:脚本可能不会关闭不是由脚本打开的窗口。

    https://developer.mozilla.org/en-US/docs/Web/API/Window.close

    【讨论】:

    • 那我如何关闭按钮上的 aspx 页面?
    • 如果你想重定向用户关闭当前页面并打开另一个页面......告诉服务器。 :) 或者,您可以从脚本打开子窗口,然后使用脚本将其关闭 :)
    【解决方案3】:

    Chrome 的 hacky 解决方法(在其内部重新打开窗口并关闭它):

    <script>
        open(location, '_self').close();
    </script>
    

    此代码适用于我的 Chrome:

    <script>
    
    function confirm_delete()
    {
        if (confirm("Are you sure you want to close?") == true) open(location, '_self').close();
        else return false;
    } 
    confirm_delete();
    
    </script>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-10-20
      • 1970-01-01
      • 1970-01-01
      • 2011-01-21
      • 1970-01-01
      • 2012-06-22
      • 2017-09-09
      • 1970-01-01
      相关资源
      最近更新 更多