Jquery mobile jQuery Mobile:如何在每次页面加载时读取查询字符串参数?

Jquery mobile jQuery Mobile:如何在每次页面加载时读取查询字符串参数?,jquery-mobile,query-string,querystringparameter,Jquery Mobile,Query String,Querystringparameter,我对jQuery和jQueryMobile完全陌生,遇到了一个问题。我有两个html页面,其中一个是项目列表。每个项目都链接到主页,并将?id=xxx添加到url。主页读取id并使用以下代码提供内容: $.urlParam = function(name){ var results = new RegExp('[\\?&]' + name + '=([^&#]*)').exec(window.location.href); return results[

我对jQuery和jQueryMobile完全陌生,遇到了一个问题。我有两个html页面,其中一个是项目列表。每个项目都链接到主页,并将?id=xxx添加到url。主页读取id并使用以下代码提供内容:

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

   console.log($.urlParam('id'));
第一次加载页面时,id是正确的。当我加载列表并选择一个不同的项目时,URL中的id会发生变化,但是控制台会打印与以前相同的id,并且内容不会更新。我认为问题可能与缓存有关。我曾尝试使用不同的函数(不带RegExp)读取id,但它产生了相同的问题


谢谢

何时调用此
urlParam
?是的,请解释何时调用url。在加载页面内容的脚本开头调用urlParam。谢谢,很好的建议,但实际上我通过在脚本中的函数之间传递变量解决了这个问题,这样就不需要查询字符串参数了。太好了!!!考虑一个场景,你有一个列表页面和一个细节页面,当你从列表导航到详细页面时,如果我们从函数传递参数,那么在细节页面上,对细节页面进行刷新或重新加载,它会使PARAM值松弛。这种情况特别发生在有移动网站时,因此我们继续进行会话存储,以便详细信息页面可以独立获取参数。我不认为刷新/重新加载经常发生在移动页面上。内容每周更新一次,只有在用户有意更新时,页面才应该更新。我理解会话存储的使用,但是变量是一个更简单的解决方案。
Why don't you use session storage (if its a mobile website) on the click event of the list item store this selected id into session storage and on the main page live get it back from storage, so you need not pass using query string, generally passing from query string is not safe, and you have limitations on how much data you can pass.