【问题标题】:Window.open + href replace and pop into new window but not using target _blankWindow.open + href 替换并弹出到新窗口但不使用目标 _blank
【发布时间】:2011-08-24 13:37:24
【问题描述】:

我遇到了一些问题,我希望测试 1 和测试 2 链接在新窗口中弹出打开, target=_blank 进入新窗口,因此弹出窗口不会' t 在 Firefox 等中作为选项卡打开。javascript 中的 "href" 用于填充测试 1 和测试 2 链接中的 href #。我做错了什么,所以我也可以在新窗口中打开弹出窗口,但不能作为目标_blank?

<p><a id="test1" href="#">Test 1</a></p>
<p><a id="test2" href="#">Test 2</a></p>

<script> 
if(chatlink_flag == 'true')
{ 
document.getElementById('test1')window.open.href('http://www.example.com/chat1/open.htm','window1','scrollbars=no,menubar=no,height=600,width=800,resizable=yes,toolbar=no,location=no,status=no'); 
document.getElementById('test2')window.open.href('http://www.example.com/chat2/open.htm','window2','scrollbars=no,menubar=no,height=600,width=800,resizable=yes,toolbar=no,location=no,status=no'); 
}else{ 
document.getElementById('test1')window.open.href('http://www.example.com/chat1/closed.htm','window1','scrollbars=no,menubar=no,height=600,width=800,resizable=yes,toolbar=no,location=no,status=no'); 
document.getElementById('test2')window.open.href('http://www.example.com/chat2/closed.htm','window2','scrollbars=no,menubar=no,height=600,width=800,resizable=yes,toolbar=no,location=no,status=no'); 
}
</script>

【问题讨论】:

    标签: javascript window href


    【解决方案1】:

    您的代码似乎没问题,只是缺少“javascript:”部分。

    因此,您可以尝试像这样执行 window.open 行:

    document.getElementById('test1').href = "javascript:window.open('http://www.yourpage.com/', 'window1', 'scrollbars=no,menubar=no,height=600,width=800,resizable=yes,toolbar=no,location=no,status=no')";
    

    【讨论】:

    • 谢谢你,因为这解决了我的问题。此外,通过添加“void”然后(打开)阻止出现 [object Window] 消息的返回值: document.getElementById('test1').href = "javascript:void(open('yourpage.com', ' window1','scrollbars=no,menubar=no,height=600,width=800,resizable=yes,toolbar=no,location=no,status=no'))";
    【解决方案2】:

    我不确定您希望该代码做什么,但以下内容应该适合您:

    <p><a id="test1" href="#" onclick="open_window(1);">Test 1</a></p>
    <p><a id="test2" href="#" onclick="open_window(2);">Test 2</a></p>
    
    <script type="text/javascript">
    
    function open_window(id) {
        if (chatlink_flag) {
            window.open('http://www.example.com/chat' + id + '/open.htm','window' + id,'scrollbars=no,menubar=no,height=600,width=800,resizable=yes,toolbar=no,location=no,status=no');
        }
        else {
            window.open('http://www.example.com/chat' + id + '/closed.htm','window' + id,'scrollbars=no,menubar=no,height=600,width=800,resizable=yes,toolbar=no,location=no,status=no');
        }
    }
    
    </script>
    

    【讨论】:

    • 不幸的是,我无法在我的链接中添加 onclicks。它是如此单调也不能。我们的链接在 cms 内进行管理,我们无法使用 onclicks。 :(
    • 在这种情况下,只需将 href 更改为“javascript:open_window(1);”、“javascript:open_window(2);”等等。或者,您可以使用 jQuery 将事件处理程序绑定到锚点。
    猜你喜欢
    • 2013-04-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多