如何使用JavaScript打开新的选项卡/窗口? [英] How do I open a new tab/window using JavaScript?
问题描述
我想在新标签/窗口中以 EXACT 的方式在target =_ blank中打开一个网址。
I want to open a URL in a new tab/window in the EXACT same manner as target="_blank" would.
我正在使用PHP条件触发以下JavaScript:
I'm using a PHP condition which triggers the following JavaScript:
<script type="text/javascript">
window.open ("http://www.google.com/","_blank", "status=1,toolbar=1");
</script>
我的问题
window.open是不相同 as target =_ blank超链接。
My problem
window.open is NOT THE SAME as target="_blank" hyperlinks.
- 这会出现弹出窗口阻止程序的问题。
- 窗口需要参数看起来像target =_ blank会产生的。
- JavaScript运行后,包含某些字体颜色文件丢失。
我的问题
我如何准确地模拟什么是由target =_ blank生成?
My question
How do I EXACTLY simulate what's produced by target="_blank"?
推荐答案
不使用JavaScript打开Window,而是使用JavaScript更新链接的href,然后触发点击链接。通过这种方式,您可以获得与用户点击链接完全相同的行为。
Instead of opening the Window from JavaScript, use JavaScript to update a link's href and then trigger a click on the link. This way you'll get the exact same behavior as the user clicking the link.
添加一个ID为 target =的页面链接_blank
。当您想要打开一个新窗口时,更新此链接的 href
,然后触发一次点击,如下所示(来自 here 。
Add a link to your page with an id and target="_blank"
. When you want to open a new window, update the href
of this link and then trigger a click, like this (from here).
function clickLink(link) {
var cancelled = false;
if (document.createEvent) {
var event = document.createEvent("MouseEvents");
event.initMouseEvent("click", true, true, window,
0, 0, 0, 0, 0,
false, false, false, false,
0, null);
cancelled = !link.dispatchEvent(event);
}
else if (link.fireEvent) {
cancelled = !link.fireEvent("onclick");
}
if (!cancelled) {
window.location = link.href;
}
}
这篇关于如何使用JavaScript打开新的选项卡/窗口?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!