Normal redirect or preload

So, on the net, I came across several ways to preload / redirect a web page.

Now the question is the correct way to handle pre-loading redirects (load the next async page while showing the current page)

$.get("page.php", function (data) { document.open(); document.write(data); document.close(); window.history.pushState("Title", "Title", "/page.php"); $.cache = {}; }, "html"); 

Or should I stay with a regular redirect?

 window.location = "page.php"; 

The next page contains a full-screen video and soundtrack (audio)

Thanks.

+6
source share
1 answer

You can use Ajax to load the next page asynchronously. The following is an example of a simple Ajax request using the GET method written in JavaScript .

AJAX means asynchronous JavaScript and XML, and for the XMLHttpRequest object as AJAX, the async parameter of the open () method must be set to true: xhr.open('get', 'send-ajax-data.php', true);

get-ajax-data.js:

 // This is the client-side script. // Initialize the Ajax request. var xhr = new XMLHttpRequest(); xhr.open('get', 'send-ajax-data.php', true); // `true` makes the request asynchronous // Track the state changes of the request. xhr.onreadystatechange = function () { var DONE = 4; // readyState 4 means the request is done. var OK = 200; // status 200 is a successful return. if (xhr.readyState === DONE) { if (xhr.status === OK) { alert(xhr.responseText); // 'This is the returned text.' } else { alert('Error: ' + xhr.status); // An error occurred during the request. } } }; // Send the request to send-ajax-data.php xhr.send(null); 

And at the end, you can use the codes below to reload or redirect page data:

 document.getElementById("myDiv").innerHTML = xhr.responseText; 
+4
source

Source: https://habr.com/ru/post/989425/


All Articles