【问题标题】:Javascript Loading a link on a new page from a functionJavascript从函数加载新页面上的链接
【发布时间】:2011-08-31 07:08:30
【问题描述】:
function loadPage(list) {
    location.href=list.options[list.selectedIndex].value
}

我有一个从列表中加载的函数:

<form name="countrySelector" id="countrySelector">
        <select name="file" size="1" onchange="loadPage(this.form.elements[0])" target="_blank" onmouseclick="this.focus()">
            <option value="empty">Select</option>
            <option value="http://www.facebook.com/">Argentina</option>

有没有办法强制这个函数在新窗口中打开链接?

【问题讨论】:

    标签: javascript location href


    【解决方案1】:
    function loadPage(list) { 
        window.open(list.options[list.selectedIndex].value);
    } 
    

    试试on jsFiddle。请注意,它可能会触发用户的弹出窗口拦截器。

    【讨论】:

    • 它不会触发浏览器的默认弹出窗口拦截器,因为窗口是由用户操作触发的。
    • @PaulPRO 它会触发我浏览器 (IE9) 的默认弹出窗口拦截器。
    • 哦,奇怪,我没想到会……这也会触发它吗? jsfiddle.net/Paulpro/sybd9
    • 它不会在带有_blank 目标的链接上触发,不会。您的示例适用于 Chrome,但似乎不适用于 Firefox 或 IE9
    • 我猜这意味着 Firefox 和 IE9 不认为 onchange 是用户操作。在这种情况下,如果 OP 想避免弹出窗口拦截器,他可能会不走运。
    【解决方案2】:

    使用window.open:

    function loadPage(list) {
        window.open(list.options[list.selectedIndex].value, "WindowName");
    }
    

    【讨论】:

      【解决方案3】:
      function loadPage(list){
          var href = list.options[list.selectedIndex].value;
          var features = 'scrollbars=no,menubar=no,height=600,width=800,resizable=yes,toolbar=no,location=no,status=no';
      
          window.open(href,'WindowName', features);
      }
      

      更多信息herehere

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-10-28
        • 1970-01-01
        • 2019-01-01
        • 1970-01-01
        • 2020-10-05
        • 2015-08-08
        • 1970-01-01
        • 2012-06-23
        相关资源
        最近更新 更多