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

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

问题描述

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



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



我的一些电话会创建一个新选项卡(首选行为),并且会导致弹出窗口。

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

page_content只是来自AJAX调用的普通HTML。报告包含标题,favicon和一些样式表头中的一些信息。



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



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



欣赏任何见解。

解决方案

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



示例:

  $(a.runReport)。click(function(evt){
//在点击处理程序中打开一个弹出窗口
//这应该在一个新窗口中打开
var popup = window.open(about:blank,myPopup);

//做一些ajax调用
$ .get(/ run / the / report,function(result){
//现在写入弹出窗口
popup.document .write(result.page_content);

//或更改位置
// popup.location ='someOtherPage.html';
});
} );


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

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 is just regular HTML from AJAX calls. Reports contain some information in the header like title, favicon, and some style sheets.

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.

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.

Appreciate any insight.

解决方案

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.

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

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

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