Get param value from a URL in jquery mobile

China☆狼群 提交于 2019-12-31 07:26:28

问题


I have got a page called servicesDetails.html that has links.The url looks like below

../Myapp/servicesDetails.html?servicetype=Advanced&serviceid=1208

The links when clicked loads an internal page called audioListPage with some params,the link looks like this

<a href="#audioListPage?audioid=123&audiotype=mp3" class="loadAudio" data-role="button" data-mini="true" data-inline="true">

Once the audioListPage is loaded the url changes to

../Myapp/servicesDetails.html?servicetype=Advanced&serviceid=1208#audioListPage?audioid=123&audiotype=mp3

I need to display the user with the audioid and audiotype when the audioListPage loads,how do i do this?

Based on a comment below I tried this

$.urlParam = function(name){
var results = new RegExp('[\\?&]' + name + '=([^&#]*)').exec(window.location.href);
return results[1] || 0;
}

$('#audioListPage').live('pagecreate',function(event) {
   console.log($.urlParam('audioid '));//this is giving me an error
});

I checked why the error was occuring and found that window.location.href is giving me only ../Myapp/servicesDetails.html?servicetype=Advanced&serviceid=1208, not the updated URL.Im not sure if Im calling the $.urlParam() at the right event


回答1:


jQM Documentation suggests using one of these plugins for parameters:

jQuery Mobile does not support query parameter passing to internal/embedded pages but there are two plugins that you can add to your project to support this feature. There is a lightweight page params plugin and a more fully featured jQuery Mobile router plugin for use with backbone.js or spine.js.

  • https://github.com/jblas/jquery-mobile-plugins/tree/master/page-params
  • https://github.com/azicchetti/jquerymobile-router

Docs: ( See Known limitations section )

  • http://jquerymobile.com/demos/1.1.0/docs/pages/page-navmodel.html



回答2:


You can just parse window.location.hash

See this example:

How can I get query string values in JavaScript?

and replace window.location.search

with window.location.hash



来源:https://stackoverflow.com/questions/11260792/get-param-value-from-a-url-in-jquery-mobile

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!