Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 492
user1441141 Avatar asked Aug 17 '12 04:08

user1441141


People also ask

How do I make a link open in a new tab instead of a new window?

Method 1: Ctrl+Click Simply press and hold the Ctrl key (Cmd on a Mac) and then click the link in your browser. The link will open in a new tab in the background.

How do I make a new tab open automatically?

If you use a mouse, simply utilizing the middle mouse button to click on a link will immediately open it in a new browser tab! Holding down the Shift key while middle-clicking also helps you switch to the tab automatically. Trackpads on Windows laptops can also open links in new tabs.


1 Answers

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';     }); }); 
like image 173
Matt MacLean Avatar answered Oct 10 '22 02:10

Matt MacLean