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

5

26

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.

Christianly answered 17/8, 2012 at 4:35 Comment(1)
Throwing a crazy idea out there: window.open('data:text/html,' + escape(page_content))? EDIT: Just tried, nope.Calcification
T
40

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';
    });
});
Timpani answered 31/1, 2013 at 17:48 Comment(7)
damn i was suspecting this would be the case (i.e. user interaction/event somehow involved), thanksLacee
You can manipulate the popup right away so you could display a "Processing" message or something until the callback(s) is complete..Timpani
@maclema: my understanding is it must be a callback/handler to a user action (otherwise such precaution would be easily 'workaroundable'), in that case i don't see where/how/why would yo udisplay such message...? but maybe i haven't quite understood your last commentLacee
Sorry, I meant if the user's action invoked some sort of ajax saving process that you needed to wait for one or more ajax requests to complete.Timpani
var popup = window.open("about:blank", "My Page"); popup.location = 'my_page.html';Rodenhouse
I found that even this is tricky when you're executing a stack of functions derived from an onClick event. Frustrated, I found chrome extension OneWindow to be of some help.Setaceous
whenever ajax is involved it is better to use .on instead of .click #9344806 reference: api.jquery.com/onDefer
A
3

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>
Acidic answered 26/3, 2013 at 3:27 Comment(0)
P
0

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

onmouseup="switchMenu(event);"

Postnatal answered 10/10, 2014 at 17:50 Comment(0)
Y
0

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.

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;
Yolandoyolane answered 4/4, 2018 at 15:33 Comment(0)
F
-1
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.

Farnesol answered 17/8, 2012 at 4:39 Comment(2)
_newtab does nothing in Chrome.Christianly
Also, I checked the settings in Chrome and unless I am blind I can't find where you set anything that controls how links open.Christianly

© 2022 - 2024 — McMap. All rights reserved.