Javascript 非pushState浏览器中的JQuery移动查询字符串问题
我试图从JQuery Mobile beta 3中的querystring中提取变量cid 普通URL的示例如下 /category.php?cid=23 JQuery移动URL示例 /#/category.php?cid=23 由于JQuery Mobile Beta 3的url重写,在大多数浏览器中,我可以使用下面的函数正常地从查询字符串中提取变量。但是IE不支持新的浏览器历史记录功能 因此,我需要一种方法从上面看到的JQuery移动Url中拉出querystring,下面我通常使用的函数无法正常工作Javascript 非pushState浏览器中的JQuery移动查询字符串问题,javascript,jquery,regex,url-rewriting,jquery-mobile,Javascript,Jquery,Regex,Url Rewriting,Jquery Mobile,我试图从JQuery Mobile beta 3中的querystring中提取变量cid 普通URL的示例如下 /category.php?cid=23 JQuery移动URL示例 /#/category.php?cid=23 由于JQuery Mobile Beta 3的url重写,在大多数浏览器中,我可以使用下面的函数正常地从查询字符串中提取变量。但是IE不支持新的浏览器历史记录功能 因此,我需要一种方法从上面看到的JQuery移动Url中拉出querystring,下面我通常使用的函数无
function getQuerystring(key) {
var re=new RegExp('(?:\\?|&)'+key+'=(.*?)(?=&|$)','gi');
var r=[], m;
while ((m=re.exec(document.location.search)) != null) r.push(m[1]);
return r;
}
我正在寻找一个替代的正则表达式,它可以在不支持history.pushState的浏览器中找到散列变量,或者完全使用其他解决方案。我在文档中搜索了这个问题的解决方案,但没有找到任何东西。我认为这将是一个JQuery移动团队已经考虑并解决的问题,我可能只是错过了一些非常明显的东西。提前感谢您的帮助。工作演示(我想)
-
主页
-
-
第2页
-
好的,这里有一个函数,它可以处理这两种情况,无论您使用的是什么浏览器,甚至是使用JQuery Mobile的方式。这个代码并不完美,我只是很高兴它现在能工作。如果我更新了函数,我会回来在这里更新它
首先,代码查看是否存在哈希。如果有,它会像查找url一样在其中查找查询字符串。如果不是,它只是检查变量的url。这段代码用于检查特定的querystring变量,而不管您在JQuery Mobile中检查它的状态如何。无论是PageCreatePageShow还是其他任何事件,jQueryMobile Beta3是否通过pushstate重写了url
function getQuerystring(name) {
var hash = document.location.hash;
var match = "";
if (hash != undefined && hash != null && hash != '') {
match = RegExp('[?&]' + name + '=([^&]*)').exec(hash);
}
else {
match = RegExp('[?&]' + name + '=([^&]*)').exec(document.location.search);
}
return match && decodeURIComponent(match[1].replace(/\+/g, ' '));
}
related:在这种情况下,该功能也会失败。
<div data-role="page" id="home">
<div data-role="content">
<ul data-role="listview" data-inset="true" data-theme="c" data-dividertheme="f">
<li data-role="list-divider">
Home Page
</li>
<li>
<!--
Pass Parm Here before the hash page
and treating link as external
-->
<a href="?cid=1234#page2" rel="external">Page 2</a>
</li>
</ul>
</div>
</div>
<!-- page 2 -->
<div data-role="page" id="page2">
<div data-role="content">
<ul data-role="listview" data-inset="true" data-theme="c" data-dividertheme="f">
<li data-role="list-divider">
Page 2
</li>
<li>
<a href="?cid=4321#home">Home Page</a>
</li>
</ul>
</div>
</div>
function getQuerystring(name) {
var hash = document.location.hash;
var match = "";
if (hash != undefined && hash != null && hash != '') {
match = RegExp('[?&]' + name + '=([^&]*)').exec(hash);
}
else {
match = RegExp('[?&]' + name + '=([^&]*)').exec(document.location.search);
}
return match && decodeURIComponent(match[1].replace(/\+/g, ' '));
}