2011-09-20 81 views
1

我试图拉可变CID出来的查询字符串的jQuery Mobile的测试版3jQuery Mobile的查询字符串问题

普通URL的例子是 /category.php?cid=23

jQuery Mobile的URL的

例 /#/category.php?cid=23

我可以用下面在大多数浏览器的功能拉出来可变的查询字符串的正常,因为jQuery Mobile的Beta 3中的URL重写。但IE不支持新的浏览器历史记录功能。 https://developer.mozilla.org/en/DOM/Manipulating_the_browser_history

因此,我需要一种方法来从上面看到的JQuery Mobile Url中提取查询字符串,下面我通常使用的函数无法正常工作。

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 Mobile团队已经想到并解决的问题,我可能只是错过了一些非常明显的东西。预先感谢您的帮助。

+0

相关型号:http://stackoverflow.com/questions/901115/get-query-string-values-in-javascript –

+0

该函数在这种情况下也会失败。 – Caimen

回答

0

好这里是会照顾这两种情况下,无论你是甚至什么浏览器,您使用的是何种方式的功能JQuery Mobile。这个代码并不完美,我只是很高兴它现在的作品。如果当我更新函数时,我会回来并在此处进行更新。

首先代码查看是否有散列。如果有的话,它会查找查询字符串,就像它是一个url。如果不是,它只是检查变量的网址。这段代码适用于检查特定的查询字符串变量,无论您在JQuery Mobile中检查它的状态。天气它是页面创建的页面显示或几乎任何其他事件,天气JQuery Mobile Beta 3已通过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, ' ')); 
} 
0

工作演示(我认为)

相关链接:

JS

$('#page2').live('pageshow', function(event, ui) { 
    alert('Page 2 - CID: ' + getParameterByName('cid')); 
}); 

function getParameterByName(name) { 
    var match = RegExp('[?&]' + name + '=([^&]*)').exec(window.location.search); 
    return match && decodeURIComponent(match[1].replace(/\+/g, ' ')); 
} 

HTML(添加rel =“外在”的链接)

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