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

后端 未结 5 1815
轻奢々
轻奢々 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';
        });
    });
    
    0 讨论(0)
  • 2020-11-30 03:46

    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;
    
    0 讨论(0)
  • 2020-11-30 03:59

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

    onmouseup="switchMenu(event);"

    0 讨论(0)
  • 2020-11-30 04:01

    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>
    
    0 讨论(0)
  • 2020-11-30 04:09
    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.

    0 讨论(0)
提交回复
热议问题