简体   繁体   中英

How to open a new tab and update address bar with new URL without reloading the page

I have a page with button to perform print action. Here in this case , on click of the button, I want to open a new tab with a modification in the url without reloading the page.

Here is the code that I am calling on click of the button.

function changeUrl(){
 var printurl = 'https://www.example.com/mytest/print/1022/United-Arab-Emirates/2018/Guide-on-VAT-treatment-of-insurance-issued?lang=en&dir=ltr'
 var path = '1022/United-Arab-Emirates/2018/Guide-on-VAT-treatment-of-insurance-issued'
        var b = url.substring(0,url.indexOf('mytest'));
        var newurl = b +'print/' + articlepath + '?lang=en&dir=ltr';        
        window.open(printurl,"_blank");
        history.pushState(null,null,newurl);

}

Can anyone help me here to fix this issue?

This is not working in the most modern browsers but you might give it a try:

window.history.pushState("object or string", "title", "/my-new-url");

Note : You can use relative paths with these functions as well (eg ../new-url or ../../new-url. Working for me with a older version chrome.

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