2015-06-19 25 views
6

所以在网络上我遇到了几种预加载/重定向网页的方法。正常重定向或预加载

现在的问题是这样的处理与预紧重定向的正确方法(装入下一页异步同时还显示了当前页)

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

或者我应该更好地与经常重定向留下来吗?

window.location = "page.php"; 

下一个页面中包含全屏视频和音轨(音频)

感谢。

+0

您可以使用Ajax:https://en.wikipedia.org/wiki/Ajax_(programming)加载下一页异步! – Behzad

+0

有用的链接:http://www.w3schools.com/json/json_http.asp – Behzad

回答

4

您可以使用Ajax加载下一页异步。 下面是使用GET方法的简单Ajax请求示例,编写于JavaScript

AJAX代表了异步JavaScript和XML,以及XMLHttpRequest对象的行为与AJAX的open()方法的async参数必须设置为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); 

,并在结束时,你可以使用下面的代码重新加载或重定向页面数据:

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

这里有一些额外的浏览器兼容性信息的情况下OP有兴趣http://caniuse.com/#feat=xhr2 – zfrisch