简体   繁体   中英

jQuery doesn't open a page in the new tab

I want to open an URL on the new tab. The following code, opens it on the same tab:

$.post('insert_home.php',{
    UPDATECSSGOLD:'selected'}).done(function(data){
        alert ('THEME SUCCESSFULLY CHANGED!');
        window.location.replace("../WebPages/Home.php");                        
});

What am I missing?

Try the following:

$.post('insert_home.php',{
     UPDATECSSGOLD:'selected'}).done(function(data){
         alert ('THEME SUCCESSFULLY CHANGED!');
         window.open("../WebPages/Home.php", '_blank');                
});

This will open geven URL in the new tab.

Description:

The open() method creates a new secondary browser window, similar to choosing New Window from the File menu. The strUrl parameter specifies the URL to be fetched and loaded in the new window. If strUrl is an empty string, then a new blank, empty window (URL about:blank) is created with the default toolbars of the main window.

$.post('insert_home.php',{
     UPDATECSSGOLD:'selected'}).done(function(data){
         alert ('THEME SUCCESSFULLY CHANGED!');
         window.open("../WebPages/Home.php", '_blank');                
});

adding _blank helps us to open the page in a new tab.

Just like in html Example of html

<a href="path" target="_blank">Click</a>

You can use something similar to:

 var win=window.open("../WebPages/Home.php", '_blank');
 win.focus();

but be aware that it is ultimately up to the browser how it will open the new page - new tab, new window or same window.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM