jQuery Mobile和“查询参数"用于hashbang导航 [英] jQuery Mobile and "query parameters" for hashbang navigation
问题描述
我正在使用jQuery Mobile,并且在一个HTML页面中只有很少的页面.打开这些页面时,我想为它们传递参数,以便它们的参数在URL中保持不变.
I am using jQuery Mobile and have few pages in one HTML page. When opening these pages, I'd like to pass parameters for them, so that their parameters are persistent in URL.
例如
<a href="#map?x=4&y=2"
它将打开,我可以在showshow事件中访问参数X和Y.
It would open and I could access parameters X and Y in beforeshow event.
这可能吗?如何?您建议使用哪种替代方法建议使用hashbangs编码参数?
Is this possible and how? What alternative means you suggest for encoding parameters with hashbangs?
推荐答案
是的,您可以具有显示的链接:
Yes, you can have links like the one you showed:
<a href="#map?x=4&y=2"> Click here </a>
然后,在放映前,您可以使用以下代码阅读以下参数:
Then, on before show you can read this params with this code:
var params = QueryStringToHash(location.hash.substr(1));
//Now you can use params.x, params.y, etc
QueryStringToHash
的定义(从此处获取)如下:
The definition of the QueryStringToHash
(got from here) is the following:
var QueryStringToHash = function QueryStringToHash (query) {
var query_string = {};
var vars = query.split("&");
for (var i=0;i<vars.length;i++) {
var pair = vars[i].split("=");
pair[0] = decodeURIComponent(pair[0]);
pair[1] = decodeURIComponent(pair[1]);
// If first entry with this name
if (typeof query_string[pair[0]] === "undefined") {
query_string[pair[0]] = pair[1];
// If second entry with this name
} else if (typeof query_string[pair[0]] === "string") {
var arr = [ query_string[pair[0]], pair[1] ];
query_string[pair[0]] = arr;
// If third or later entry with this name
} else {
query_string[pair[0]].push(pair[1]);
}
}
return query_string;
};
希望这会有所帮助.干杯
Hope this helps. Cheers
这篇关于jQuery Mobile和“查询参数"用于hashbang导航的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!