简体   繁体   English

在页面上记录数据并实现pushstate()的最佳方法

[英]The best way to record data on a page and implement pushstate()

My website has a single page which the content is dynamically changed within one article element. 我的网站只有一个页面,该页面的内容在一个article元素内动态更改。

<article id="dynamicArtile"></article>

I am trying to use pushState() and a popstate event listener to create history entries so my website functions with the back and forward buttons on the browser. 我正在尝试使用pushState()popstate事件侦听器创建历史记录条目,因此我的网站在浏览器上具有后退和前进按钮的功能。

I would like to store the content of the page in a stateObj variable like so: 我想将页面内容存储在stateObj变量中,如下所示:

var stateObj = {content: dynamicArticle.innerHTML}

I am not very sure on how to reload the previous page using the stateObj variable. 我不太确定如何使用stateObj变量重新加载上一页。

Any pointers or examples would be great. 任何指针或示例都很好。 Staying away from JQuery if possible 尽可能远离JQuery

 function loadpage(url,f) { $.ajax({ url: '/ajax'+url, dataType: 'json' }).done(function(res){ $('.dynamicArtile').html(res.content); if(typeof f === 'undefined') window.history.pushState({path:url},'',url); }); } $(function(){ $('a').click(function(e) { // only links that point to your site var url = $(this).attr('href'); loadpage(url); return false; }); $(window).bind('popstate',function(){ loadpage(location.pathname + location.search,1); }); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 

i think you don't use stateObj variable 我认为你不使用stateObj变量

sorry for my terrible English. 对不起,我的英语不好。

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

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