简体   繁体   English

带Ajax的Backbutton加载内容

[英]Backbutton With Ajax loading Content

This will load the content to the page and change the URL. 这会将内容加载到页面并更改URL。 The problem is that the back button and the forward won't load the content agin. 问题在于后退按钮和前进按钮不会加载内容开始。 They only change the URL. 他们只更改URL。 How can I make them load the function? 如何让他们加载功能?

 <a onclick="load_content('p1');" >Page1</a>
 <a onclick="load_content('p2');" >Page2</a>



function load_content(name)
    {

    window.history.pushState(null, null, "/?page=" + name + "");

    var xmlhttp;
    if (window.XMLHttpRequest)
      {// code for IE7+, Firefox, Chrome, Opera, Safari
      xmlhttp=new XMLHttpRequest();
      }
    else
      {// code for IE6, IE5
      xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
      }
    xmlhttp.onreadystatechange=function()
      {
      if (xmlhttp.readyState==4 && xmlhttp.status==200)
        {

        document.getElementById("response").innerHTML=xmlhttp.responseText;

        }
      }
    xmlhttp.open("GET","load_content.php?name=" + name + "",true);
    xmlhttp.send();

    }

history.pushState ,保存所有动态的相关数据,然后侦听popState事件并替换所有元素,就像使用AJAX一样。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM