如何使用JavaScript打开新的选项卡/窗口? [英] How do I open a new tab/window using JavaScript?

查看:403
本文介绍了如何使用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.


  1. 这会出现弹出窗口阻止程序的问题。

  2. 窗口需要参数看起来像target =_ blank会产生的。

  3. 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屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆