Open a new tab in the background?

前端 未结 5 1529
闹比i
闹比i 2020-11-22 10:07

Using javascript, I want to open a new page in a different tab, but remain focused on the current tab. I know I can do it like this:

open(\'http://example.co         


        
5条回答
  •  南旧
    南旧 (楼主)
    2020-11-22 10:41

    I did exactly what you're looking for in a very simple way. It is perfectly smooth in Google Chrome and Opera, and almost perfect in Firefox and Safari. Not tested in IE.


    function newTab(url)
    {
        var tab=window.open("");
        tab.document.write(""+document.getElementsByTagName("html")[0].innerHTML+"");
        tab.document.close();
        window.location.href=url;
    }
    

    Fiddle : http://jsfiddle.net/tFCnA/show/

    Explanations:
    Let's say there is windows A1 and B1 and websites A2 and B2.
    Instead of opening B2 in B1 and then return to A1, I open B2 in A1 and re-open A2 in B1.
    (Another thing that makes it work is that I don't make the user re-download A2, see line 4)


    The only thing you may doesn't like is that the new tab opens before the main page.

提交回复
热议问题