Javascript 在将其放入历史记录之前更改 url 参数

发布于 01-05 06:28 字数 1042 浏览 3 评论 0原文

我正在使用数据表 JQuery 插件。我将它与 AJAX 和分页(服务器端)一起使用。每行都包含一个指向该记录的详细视图的链接。单击该链接然后单击后退按钮时,我想返回到上一页。 (注意:不能使用数据表自身的状态保存)。

如果您可以在将当前 url 放入历史记录之前添加 url 参数,则可以实现此目的。

这可以做到吗?纯 JS 或 JQuery 并不重要。

(注意:我是做这类事情的新手,已经阅读过有关使用 # 的内容,但从未这样做过,所以如果您建议,请提供一个适用于我的问题的示例)

编辑:

JQuery 插件 bbq 的非常基本指南 (http://benalman.com/projects/jquery-bbq-plugin/)

假设 URL 的哈希值为 #a=2&b=test,您可以通过以下方式获取值:

//true = optional converts 2 to an integer, false to boolean,...
var a = $.bbq.getState('a',true);

您可以通过以下方式更改/添加值:

var hash = "a=3&b=hello";
$.bbq.pushState(hash);

pushState如果 hashchange 事件被绑定,则触发该事件。要绑定事件,请在 doucment.ready 函数中执行以下操作:

// called when # part of URL is modified
$(window).bind( 'hashchange', function( event ) {
    var hash = event.fragment; // full hash as string
    var a = event.getState('a', true ); // value for a
    // here do meaningful action like an AJAX request using the hash parameters    
});

I'm using the datatables JQuery plugin. I use it with AJAX and pagination (server-side). Each row contains a link to a detailed view of that record. When clicking in that link and then on the back button I want to return to the previous page. (Note: datatables own state-saving can't be used).

This could be achieved If you could add a url parameter to the current url before it is put into the history.

Can this be done? pure JS or JQuery does not really matter.

(Note: I'm quite new do this kind of stuff, have read about using the # for this but never done it so if you recommend that please provide an example applicable to my problem)

EDIT:

Very basic Guide for JQuery plugin bbq
(http://benalman.com/projects/jquery-bbq-plugin/)

Assuming URL has a hash of #a=2&b=test, you can get the values by:

//true = optional converts 2 to an integer, false to boolean,...
var a = $.bbq.getState('a',true);

You can change/add a value by:

var hash = "a=3&b=hello";
$.bbq.pushState(hash);

pushState fires hashchange Event in case it is bound. To bind the event but following in doucment.ready function:

// called when # part of URL is modified
$(window).bind( 'hashchange', function( event ) {
    var hash = event.fragment; // full hash as string
    var a = event.getState('a', true ); // value for a
    // here do meaningful action like an AJAX request using the hash parameters    
});

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

带刺的爱情2025-01-12 06:28:40

是的,HTML5 History API 可以让您做到这一点。查看 MDN 上的操纵浏览器历史记录文章。

下面是一个向当前 URL 添加参数并替换当前历史记录条目的示例:

 var newURL = location.href + (location.search ? "&" : "?") + "myParameter=myValue";
 history.replaceState({}, '', newURL);

History API 是一项 HTML5 功能,不幸的是 并非所有浏览器都支持。然而,有一个名为 history.js 的 polyfill,声称可以为所有浏览器带来支持。

更新:

您可能想要查看现有的解决方案和 jQuery 插件,而不是滚动自己的 AJAX 历史记录实现。看看什么是最好的后退按钮 jQuery 的答案插件? 问题。

Yes, the HTML5 History API allows you to do just that. Check out the Manipulating the Browser History article at MDN.

Here is an example that adds a parameter to the current URL and replaces the current history entry:

 var newURL = location.href + (location.search ? "&" : "?") + "myParameter=myValue";
 history.replaceState({}, '', newURL);

The History API is an HTML5 feature and unfortunately not supported on all browsers. However, there is a polyfill named history.js, which claims to bring support to all browsers.

UPDATE:

Instead of rolling your own AJAX history implementation you would probably want to look at existing solutions and jQuery plugins. Have a look e.g. at the answers to the What is the best back button jQuery plugin? question.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文