Javascript 强制 window.open() 在 chrome 中创建新选项卡

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11999837/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 07:58:10  来源:igfitidea点击:

Force window.open() to create new tab in chrome

javascriptgoogle-chrometabs

提问by user1441141

I use window.open to populate a new window with varying content. Mostly reports and stored HTML from automated processes.

我使用 window.open 来填充具有不同内容的新窗口。主要是从自动化流程中报告和存储 HTML。

I have noticed some very inconsistent behavior with Chrome with respect to window.open().

我注意到 Chrome 在 window.open() 方面有一些非常不一致的行为。

Some of my calls will create a new tab (preferred behavior) and some cause popups.

我的一些调用会创建一个新选项卡(首选行为),有些会导致弹出窗口。

var w = window.open('','_new');
w.document.write(page_content);

page_content is just regular HTML from AJAX calls. Reports contain some information in the header like title, favicon, and some style sheets.

page_content 只是来自 AJAX 调用的常规 HTML。报告在标题中包含一些信息,如标题、网站图标和一些样式表。

In IE9 the code does cause a new tab instead of a pop-up, while Chrome flatly refuses to show the content in question in a new tab. Since the content is sensitive business data I cannot post it here. I'll answer questions if I can.

在 IE9 中,代码确实会导致一个新选项卡而不是弹出窗口,而 Chrome 断然拒绝在新选项卡中显示有问题的内容。由于内容是敏感的商业数据,我不能在这里发布。如果可以,我会回答问题。

I know some people will say this is behavior left up to the user, but this is an internal business platform. We don't have time to train all the users on how to manage popups, and we just need it to be in a new tab. Heck, even a new window would be preferable to the popup since you cannot dock a popup in Chrome. Not to mention none of the popup blocking code would affect it.

我知道有人会说这是留给用户的行为,但这是一个内部业务平台。我们没有时间培训所有用户如何管理弹出窗口,我们只需要将它放在一个新选项卡中。哎呀,即使是新窗口也比弹出窗口更可取,因为您无法在 Chrome 中停靠弹出窗口。更不用说任何弹出窗口阻止代码都不会影响它。

Appreciate any insight.

欣赏任何见解。

回答by Matt MacLean

window.open must be called within a callback which is triggered by a user action (example onclick) for the page to open in a new tab instead of a window.

window.open 必须在由用户操作(例如 onclick)触发的回调中调用,以便在新选项卡而不是窗口中打开页面。

Example:

例子:

$("a.runReport").click(function(evt) {
    // open a popup within the click handler
    // this should open in a new tab
    var popup = window.open("about:blank", "myPopup");

    //do some ajax calls
    $.get("/run/the/report", function(result) {
        // now write to the popup
        popup.document.write(result.page_content);

        // or change the location
        // popup.location = 'someOtherPage.html';
    });
});

回答by buddy

You can try this:

你可以试试这个:

<a href="javascript:openWindow();">open a new tab please</a>

<script>
    function openWindow(){
        var w = window.open("about:blank");
        w.document.write("heheh new page opened");
    }
</script>

回答by Felipe.Silva

Is very easy, in order to force Chrome to open in a new tab, use the onmouseup event

很简单,为了强制Chrome在新标签页打开,使用onmouseup事件

onmouseup="switchMenu(event);"

onmouseup="switchMenu(event);"

回答by int-i

I have tried this and it worked fine in chrome. If opening a new tab is on a user action(such as a mouse click) this will work fine without any issues. But if the code is executed in another script block, you may need to enable pop-ups in chrome. Looks like this is to handle all the click-bait sites.

我试过这个,它在 chrome 中运行良好。如果在用户操作(例如单击鼠标)上打开新选项卡,这将正常工作而不会出现任何问题。但如果代码在另一个脚本块中执行,则可能需要在 chrome 中启用弹出窗口。看起来这是为了处理所有点击诱饵网站。

let newTab = window.open('', '_blank');
if (this.viewerTab == null) {
  console.log("opening in new tab to work, pop-ups should be enabled.");
  return;
}

................

newTab.location.href = viewerUrl;

回答by DanielCW

window.open('page.html', '_newtab');

Specify the '_newtab'. This works in IE and FF, and should work in Chrome. But if the user has things configured to not open in new tab then not much you can do.

指定“_newtab”。这适用于 IE 和 FF,并且应该适用于 Chrome。但是,如果用户将内容配置为不在新选项卡中打开,那么您无能为力。