Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
333 views
in Technique[技术] by (71.8m points)

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

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...