Window.open + href 替换并弹出到新窗口中,但不使用目标_blank

Window.open + href replace and pop into new window but not using target _blank

本文关键字:blank 目标 窗口 替换 href open 新窗口 Window      更新时间:2023-09-26

我被困在我希望测试 1 和测试 2 链接在新窗口中弹出的东西上,而不是 target=_blank 进入新窗口,这样弹出窗口就不会在 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.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')";

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

<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>