将URL参数保存到变量中

时间:2016-01-10 11:51:57

标签: javascript

如何将“uname”和“date”的值保存到以下网址的javascript变量中?

文件:/// d:/Android/Projects/ipack-schedule/www/visit.html UNAME =安培;日期= 10/01/2016%2013:00:00

2 个答案:

答案 0 :(得分:0)

var uri = "file:///D:/Android/Projects/ipack-schedule/www/visit.html?uname=&date=10/01/2016%2013:00:00"

var vars = getVars(uri)
document.write(JSON.stringify(vars))

function getVars(uri) {
  var s = uri.split("?")
  if (s.length == 1) return []

  var parts = s[1].split("&")
  return parts.map(function(el) {
    return el.split("=").map(function(el){return decodeURIComponent(el)})
  })
}

答案 1 :(得分:0)

试试这个:(在您的文件档案的js脚本中包含此代码:/// D:/Android/Projects/ipack-schedule/www/visit.html?uname =& date = 10/01/2016 %2013:00:00)

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