Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Web Technology by (20.3k points)

Is there a way I can modify the URL of the current page without reloading the page?

I would like to access the portion before the # hash if possible.

I only need to change the portion after the domain, so it's not like I'm violating cross-domain policies.

 window.location.href = "www.mysite.com/page2.php";  // Sadly this reloads

1 Answer

0 votes
by (40.7k points)

This solution will be valid in Chrome, Safari, Firefox 4+, and Internet Explorer 10pp4+!

For more information, refer to this: Updating address bar with new URL without hash or reloading the page

For Example:

 function processAjaxData(response, urlPath){

     document.getElementById("content").innerHTML = response.html;

     document.title = response.pageTitle;

     window.history.pushState({"html":response.html,"pageTitle":response.pageTitle},"", urlPath);

 }

Now, you can use window.onpopstate to detect the back/forward button navigation:

window.onpopstate = function(e){

    if(e.state){

        document.getElementById("content").innerHTML = e.state.html;

        document.title = e.state.pageTitle;

    }

};

Related questions

0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
asked Aug 13, 2019 in Web Technology by Sammy (47.6k points)
0 votes
1 answer
asked Aug 10, 2019 in Web Technology by Sammy (47.6k points)
0 votes
1 answer

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...