简体   繁体   English

使用ajax + jQuery分页时如何更改URL路径

[英]How to change URL path when paging using ajax + jQuery

I am using ajax post requests for doing paging on a feed in my site. 我正在使用ajax发布请求来对网站中的供稿进行分页。 When getting the post request data I am reforming the page by clearing previous data and rendering the new data that came from the request. 在获取发布请求数据时,我正在通过清除先前数据并呈现来自请求的新数据来重新设计页面。 I want to be able to change the URL as well so saving the new page link will get the user to the current page. 我还希望能够更改URL,因此保存新页面链接将使用户进入当前页面。

Example: 例:

  1. User on page example.com/feed - seeing content of page #1 网页example.com/feed上的用户-查看第1页的内容
  2. User clicking to get to page #2 -> ajax post is send and data on the page is changed using js (no refresh) 用户单击以转到页面#2->发送ajax帖子,并使用js更改页面上的数据(不刷新)
  3. URL is still example.com/feed but the content is of example.com/feed?page=2 URL仍为example.com/feed,但内容为example.com/feed?page=2

How can I set the URL to point to the new page without triggering a refresh (no redirect) ? 如何设置URL指向新页面而不触发刷新(无重定向)?

I am using Nodejs + express. 我正在使用Nodejs + express。

You can use history pushstate but some browsers does not support. 您可以使用历史记录pushstate,某些浏览器不支持。

history.pushState({id: 'SOME ID'}, '', 'myurl.html');

And don't forget about window.onpopstate, it pops if user clicks back button. 而且不要忘了window.onpopstate,如果用户单击“后退”按钮,它将弹出。

Redirect the user to an anchor point. 将用户重定向到锚点。

<a href="#2" onclick="loadPage(2);">Page 2</a>

And in your document.ready: 并在您的document.ready中:

if (window.location.hash.length > 1){
  var pageNumber = window.location.hash.substring(1);
  loadPage(parseInt(pageNumber));
} else{
  loadPage(0);
}

I understand you are aiming at a single page application. 我了解您的目标是单页应用程序。

While keeping the url is nice, note you might want distinct urls for directly accessing different parts of your application. 虽然保留该URL很好,但请注意,您可能需要不同的URL来直接访问应用程序的不同部分。 Still, you can load content with AJAX and keep a smooth application. 尽管如此,您仍可以使用AJAX加载内容并保持流畅的应用程序。 The way to go is using the hash part of the location. 解决方法是使用位置的hash部分。

The Sammy.js framework gives you a nice base to build upon, you can try it out. Sammy.js框架为您提供了一个不错的基础,您可以尝试一下。

I don't believe it is possible to change the query part of the URL without triggering a refresh (probably due to security issues). 我不相信不触发刷新就可以更改URL的查询部分(可能是由于安全问题)。 However you may change the anchor and use an event listener to detect when the anchor is being changed. 但是,您可以更改锚,并使用事件侦听器检测何时更改锚。

//Listener
$(window).on('hashchange', function() {
   if(loaction.hash.length > 1) {
      //The anchor has been changed.
      loadPageWithAjax("example.com/feed.php?page=" + location.hash.substring(1));
   } else {
      //Load standard page
      ...
   }
});

Change the anchor to load new feed 更改锚点以加载新的提要

<a href="#2">Page 2</a>

Remember to not use an anchor that is used as an id, since this makes the browser scroll to that element. 请记住不要使用用作ID的锚,因为这会使浏览器滚动到该元素。

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

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