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.

VoY
  • 5,479
  • 2
  • 37
  • 45
user1441141
  • 606
  • 2
  • 9
  • 21
  • Throwing a crazy idea out there: `window.open('data:text/html,' + escape(page_content))`? EDIT: Just tried, nope. – ephemient Aug 17 '12 at 05:05

5 Answers5

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';
    });
});
Matt MacLean
  • 19,410
  • 7
  • 50
  • 53
  • damn i was suspecting this would be the case (i.e. user interaction/event somehow involved), thanks – Jaroslav Záruba Feb 18 '13 at 14:32
  • You can manipulate the popup right away so you could display a "Processing" message or something until the callback(s) is complete.. – Matt MacLean Feb 18 '13 at 20:23
  • @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 comment – Jaroslav Záruba Feb 19 '13 at 09:11
  • 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. – Matt MacLean Feb 20 '13 at 18:06
  • 1
    var popup = window.open("about:blank", "My Page"); popup.location = 'my_page.html'; – shasi kanth Mar 05 '13 at 10:51
  • 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. – Ross Jul 28 '14 at 01:22
  • whenever ajax is involved it is better to use .on instead of .click http://stackoverflow.com/questions/9344306/jquery-click-doesnt-work-on-ajax-generated-content reference: http://api.jquery.com/on/ – happy Oct 04 '15 at 19:35
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>
Pang
  • 9,564
  • 146
  • 81
  • 122
buddy
  • 39
  • 1
0

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

onmouseup="switchMenu(event);"

MillaresRoo
  • 3,808
  • 1
  • 31
  • 37
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;
int-i
  • 661
  • 1
  • 12
  • 28
-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.

Hkachhia
  • 4,463
  • 6
  • 41
  • 76
DanielCW
  • 149
  • 1
  • 14