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

后端 未结 5 1857
轻奢々
轻奢々 2020-11-30 03:32

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 beha

5条回答
  •  不知归路
    2020-11-30 03:46

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

提交回复
热议问题