jQuery 移动:URL 查询字符串不会改变 [英] jQuery mobile: URL query string does not change
问题描述
我在玩 jQuery Mobile 时遇到了一些(对我来说)奇怪的行为.
I'm playing around with jQuery Mobile and ran into some (for me) strange behavior.
我有一堆链接,每个链接都指向同一个 jQM 页面,#otherpage
,但 URL 查询字符串的值不同,例如 #otherpage?q=foo
、#otherpage?q=bar
等等.对其他页面的更改工作正常,但查询字符串在单击之间保持不变,因此如果我首先单击 #otherpage?q=foo
的链接,然后返回第一页,所有后续页面对 #otherpage
的更改将具有 q=foo
,无论当前点击的链接的 href 是什么.
I have a bunch of links each pointing to the same jQM page, #otherpage
, but with different values for the URL query string, like #otherpage?q=foo
, #otherpage?q=bar
, and so on. The change to the other page works fine but the query string sticks between clicks, so if I first click the link to #otherpage?q=foo
, and then goes back to the first page, all subsequent page changes to #otherpage
will have q=foo
, no matter what the currently clicked link's href says.
jsFiddle 似乎不支持 jQM,所以我在这里举了一个例子:http://cpak.se/dump/location-search-test.html
jsFiddle didn't seem to have support for jQM so I put an example here: http://cpak.se/dump/location-search-test.html
我已经在 Mac 上的 Chrome 和 Safari 中试过了.
I've tried this in Chrome and Safari on Mac.
我使用查询字符串在页面之间传递简单数据,因为我将其他代码挂接到 pagechange 事件中,这或多或少不知道早期代码可能做了什么.如果我不能让这个工作,我将不得不找到另一种方式来传递数据......:P
I use the query string to pass simple data between pages since I have other code hooked into the pagechange events, that is more or less unaware of what earlier code might have done. If I can't get this working I'll have to find another way to pass data around... :P
干杯!
/克里斯托弗
推荐答案
我已经查找了这个问题,因为它确实看起来有点奇怪.有一个悬而未决的问题:https://github.com/jquery/jquery-mobile/issues/2859
I've looked up for this issue because it seems a little bit weird indeed. There is a open issue about this: https://github.com/jquery/jquery-mobile/issues/2859
Atm jQuery Mobile 不建议使用查询参数:
Atm jQuery Mobile doesn't recommend using query parameters:
我们目前不支持查询参数作为哈希的一部分片段,尽管这是经常讨论的问题在内部并且在功能请求列表中.
We don't currently support query parameters as part of the hash fragment, though this is something that's been discussed frequently internally and is on the feature request list.
他们建议使用如下插件:
They advise to use a plugin like:
- https://github.com/jblas/jquery-mobile-plugins/tree/master/page-params
- https://github.com/azicchetti/jquerymobile-router
但是,由于 DOM 中活动页面的 data-url 确实发生了变化,因此有一个解决方法,您可以使用 $(".ui-page-active").attr("data-url");
我已经在你的网站上测试过了,它给了我所有 3 个唯一的 URL
However, there is a workaround since the data-url of the active page in the DOM DOES change, you can retrieve this with $(".ui-page-active").attr("data-url");
I've tested this on your website and it gave me all 3 uniques URLs
这篇关于jQuery 移动:URL 查询字符串不会改变的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!