The best example of what I'm trying to do is YouTube's navigation system. If you click on a link to a video a red bar at the top of the page appears indicating how much of the requested page has loaded. When the requested page is finished loading it instantly replaces the contents of the previous page and the video starts to load. Several things are happening to make this as seamless as possible.
I modified the URL with:
window.history.pushState("object or string", "Title", "/new-url");
I attempted to load the requested page with:
$.("html").load("newPage.php", function(){});
This would replace the entire previous page's code with the new pages code. Several issues with this:
Back and forth navigation works with the URL, but an event handler must detect this and load the proper page. I used this:
$(window).on('popstate', function() {
$('html').load("../"+window.location.pathname, {page:"account"});
});
$.load()
can't(shouldn't) load any new scripts tags script tags because Synchronous XMLHttpRequest on the main thread is deprecated
What I'd like to be able to do:
Sources:
Updating address bar with new URL without hash or reloading the page
Load HTML page dynamically into div with jQuery
JS or Jquery browser back button click detector
Synchronous XMLHttpRequest on the main thread is deprecated
Approach: We can use window. location property inside the script tag to forcefully load another page in Javascript. It is a reference to a Location object that is it represents the current location of the document. We can change the URL of a window by accessing it.
In JavaScript, page redirection is simple. The window object's location object is a property. A web page can be redirected in a number of ways.
This following post might help you.
Curious about the new way YouTube is loading pages
You can check if a js library called history.js can help you achieve what you need.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With