javascript tutorial - [Solved-5 Solutions] Modify the URL without reloding the page - javascript - java script - javascript array
Problem:
Here's the most cross-browser solution.
This is better than the accepted answer because it uses native Object.keys if exists. Thus, it is the fastest for all modern browsers.
Solution 1:
This can now be done in Chrome, Safari, FF4+, and IE10pp4+! Example:
We can then use window.onpopstate
to detect the back/forward button navigation:
Solution 2:
HTML5 introduced the history.pushState()
and history.replaceState()
methods, which allow we to add and modify history entries, respectively.
Solution 3:
We can also use HTML5 replaceState
if we want to change the url but don't want to add the entry to the browser history:
This would 'break' the back button functionality. This may be required in some instances such as an image gallery (where we want the back button to return back to the gallery index page instead of moving back through each and every image we viewed) whilst giving each image its own unique url.
Solution 4:
Solution 5:
Here is my solution: (newUrl is our new url which we want to replace current one)