jQuery Mobile 和“查询参数"用于 hashbang 导航 [英] jQuery Mobile and "query parameters" for hashbang navigation

查看:19
本文介绍了jQuery Mobile 和“查询参数"用于 hashbang 导航的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 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"

它会打开,我可以在 beforeshow 事件中访问参数 X 和 Y.

It would open and I could access parameters X and Y in beforeshow event.

这可能吗?怎么做?您建议使用 hashbang 对参数进行编码的替代方法是什么?

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 的定义(来自 here) 如下:

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆