Javascript 当我使用jQuery Mobile时,我可以通过URL传递信息吗?

Javascript 当我使用jQuery Mobile时,我可以通过URL传递信息吗?,javascript,jquery,html,jquery-mobile,hash,Javascript,Jquery,Html,Jquery Mobile,Hash,我有一个移动应用程序,它打开一个应用程序内浏览器,使用URL将信息传递给我的服务器,比如deviceID 例如,浏览器将打开网页(jquery Mobile):www.myserver.com/doWork.html#deviceID 在doWork.html文件中使用JavaScript的服务器部分,我得到如下设备ID: var userId = window.location.hash.substring(1); 我可以使用散列#传递信息吗?在jquery mobile中,当有人使用多页模

我有一个移动应用程序,它打开一个应用程序内浏览器,使用URL将信息传递给我的服务器,比如deviceID

例如,浏览器将打开网页(jquery Mobile):
www.myserver.com/doWork.html#deviceID

在doWork.html文件中使用JavaScript的服务器部分,我得到如下设备ID:

var userId = window.location.hash.substring(1);
我可以使用
散列#
传递信息吗?在jquery mobile中,当有人使用
多页模板结构时,散列#用于在页面之间进行更改。所以我恐怕我应该用别的东西,比如问号
(?)


或者它非常好?

要将变量传递给服务器,您应该避免使用#符号,因为无论您使用的框架是什么,此符号都用于其他目的,要在GET请求中将信息传递给服务器,您应该使用?symbol,类似这样的东西应该可以做到:www.myserver.com/doWork.html?deviceID=1233455

否。停止使用#进行数据传输。让jQM自己做吧。不要打扰它。使用查询字符串(在url中添加)我的建议是停止使用查询字符串(?tags)和#tags将数据发送到下一页。使用
localStorage
处理它。与查询字符串相比,它更安全,因为用户不会看到URL的更改,所以您的敏感数据会被隐藏,至少在一定程度上是这样
localStorage
是HTML5的API,它类似于为每个域留出的临时存储。此数据将一直保留,直到缓存中的数据被清除。假设您有一个定位标记,该标记指向dowork.html

<a href="doWork.html" data-role="button">Go to Do work</a>
然后,在另一个页面的
pageinit
(或任何事件)中,从存储器中获取设备id,并向服务器发送ajax调用

//assuming #dowork is the id of a div with data-role="page"
$(document).on("pageinit", "#dowork", function() { 
  //get from storage
  var deviceId = localStorage["dId"];
  //make ajax call or server call with deviceId here
});

但是,如果您仍要为此使用URL。我在那边已经给出了一个相当不错的答案

所以你建议我做的就是改变我附加的#符号?它会起作用吗?命令是一样的吗?window.location.hash.substring(1)?否,该命令将需要更改为此位置。search.substring(1)
$(document).on("click", "a", function(e) //use a class or ID for this instead of just "a"
  //prevent default
  e.preventDefault();
  //get device id from tag attribute
  var deviceId = $(this).data("deviceid");
  //set it in localStorage 
  localStorage["dId"] = deviceId;
  //redirect 
  $.mobile.changePage(this.href);
});
//assuming #dowork is the id of a div with data-role="page"
$(document).on("pageinit", "#dowork", function() { 
  //get from storage
  var deviceId = localStorage["dId"];
  //make ajax call or server call with deviceId here
});