2011-09-20 72 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>