How to open a new tab in separate thread with JavaScript? (chrome)

谁说我不能喝 提交于 2020-12-30 06:00:36

问题


Is it possible to open a new popup tab that would run in a separate thread? To be more specific, if I create a new popup tab and start debugging in that new tab, tab which contains link will also pause javascript until I click resume in a new tab. What I want to achieve is to create a new tab that is separated so I can debug it while parent tab continues running. I have this problem using Chrome browser. Note that this works fine in Firefox (haven't tested in other browsers).


回答1:


Usually chrome forces new window to run on the same Process ID. But, there are techniques which allows sites to open a new window without forcing it into the same process:

Use a link to a different web site that targets a new window without passing on referrer information.

<a href="http://differentsite.com" target="_blank" rel="noreferrer">Open in new tab and new process</a>

If you want the new tab to open in a new process while still passing on referrer information, you can use the following steps in JavaScript:

  • Open the new tab with about:blank as its target.
  • Set the newly opened tab's opener variable to null, so that it can't access the original page.
  • Redirect from about:blank to a different web site than the original page.

For example:

var w = window.open();
    w.opener = null;
    w.document.location = "http://differentsite.com/index.html";

Technically the original website still has access to the new one through w, but they treat .opener=null as a clue to neuter the window.

Source: https://bugzilla.mozilla.org/show_bug.cgi?id=666746




回答2:


The current version of Chrome does not appear to use a separate thread when using the null opener trick that domagojk referenced. However, if you're in a javascript handler you can still take advantage of the noreferrer link trick he mentions:

        var e = document.createElement("a");
        e.href="/index.html";
        e.target="_blank";
        e.rel = "noreferrer";
        e.click();



回答3:


Have you tried using Web Workers? Not sure about support, but they're supposed to offer parallel JS execution functionality. See here and here.




回答4:


While not exactly an answer, to me it is the best answer. The print dialog should not be blocking.

I have reported this as a bug and given a test case. Show your support at here - https://bugs.chromium.org/p/chromium/issues/detail?id=1023161&q=ryein%20goddard&colspec=ID%20Pri%20M%20Stars%20ReleaseBlock%20Component%20Status%20Owner%20Summary%20OS%20Modified

I think once Chromium fixes this bug we won't have to worry about it any more. It is really the only choice at this point.

So to me the answer is we need Google/Chromium to fix this issue.



来源:https://stackoverflow.com/questions/39574000/how-to-open-a-new-tab-in-separate-thread-with-javascript-chrome

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!