强制 window.open() 在 chrome 中创建新标签 [英] Force window.open() to create new tab in chrome

查看:20
本文介绍了强制 window.open() 在 chrome 中创建新标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

I have noticed some very inconsistent behavior with Chrome with respect to 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 只是来自 AJAX 调用的常规 HTML.报告在标题中包含一些信息,如标题、网站图标和一些样式表.

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

在 IE9 中,代码确实会导致新标签页而不是弹出窗口,而 Chrome 则坚决拒绝在新标签页中显示相关内容.由于内容是敏感的业务数据,我无法在此处发布.如果可以,我会回答问题.

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.

我知道有些人会说这是留给用户的行为,但这是一个内部业务平台.我们没有时间培训所有用户如何管理弹出窗口,我们只需要将它放在新标签中即可.哎呀,即使是新窗口也比弹出窗口更可取,因为您无法在 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.

欣赏任何见解.

推荐答案

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

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.

例子:

$("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';
    });
});

这篇关于强制 window.open() 在 chrome 中创建新标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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