我怎样才能在
中专门获取所有这些查询字符串文件:/// K:?/CKaing_C20_A01_Casino2/game.html首先+名称= Testfirst&安培;最后+名称= Testlast&安培; PNUM = 123-456-7890&安培;邮编= A1A + 1A1&安培; startMoney = 5000
例如,我想获取Testfirst,然后将其分配给变量,以便稍后可以使用它。与其他人一样。
这是我到目前为止删除所有+,=
的内容var formData = location.search;
formData = formData.substring(1, formData.length);
while (formData.indexOf("+") != -1) {
formData = formData.replace("+", " ");
}
formData = unescape(formData);
var formArray = formData.split("&");
for (var i=0; i < formArray.length; ++i) {
document.writeln(formArray[i] + "<br />");
}
答案 0 :(得分:1)
var splitSearch = JSON.parse("{\""+(location.search.substr(1).replace(/\=/g,"\"\:\"").replace(/\&/g,"\", \""))+"\"}")
我为一个使用稀有(&#34; /?&#34;)分隔符的网页制作了一个。
http://example.com/?a=0&b=bee/?c=third
第一个适用于像这样的网址
如果您希望将其用于传统位置:
splitSearch.pnum
splitSearch["pnum"]
定义splitSearch后,您可以获得&#34; pnum&#34;像这样的字符串:
var splitSearch = JSON.parse("{\""+(location.search.substr(1).replace(/(\=)|(\&)|(\/\?)/g, function(k) {
var rtn=k;
if (k == "\=") rtn="\"\:\"";
else if ((k == "\&") /*|| (k == "\/\?")*/) rtn="\",\"";
return rtn;
})+"\"}"))
<小时/> 另一种获得它的方法:
if(elders)
h2 Your Elders
for item in elders
h3= item
script(async='', type='text/javascript').
var username = "#{item}";
var loadMap = function(){
$.get(username+'/getGeoData/', function(data){
//geofence configuration
console.log("get request for: " +username)
var geofences = {};
geofences['test'] = {
center: new google.maps.LatLng(data.gfLat,data.gfLon),
radius: data.gfRadius * 1000
};
//map options: centered @ current location
var current_lat = data.mostRecentLocation.latitude;
var current_lon = data.mostRecentLocation.longitude;
var currentLocation = new google.maps.LatLng(current_lat,current_lon);
var myOptions = {
zoom: 9,
center: currentLocation
};
//initialize 3 maps
var maps = [];
var cm = 'current_map_'+username;
var string_cm = String(cm);
var current_map = new google.maps.Map(document.getElementById("current_map_"+"#{item}"),
myOptions);
maps.push(current_map);
var daily_map = new google.maps.Map(document.getElementById("daily_map_"+"#{item}"),
myOptions);
maps.push(daily_map);
var weekly_map = new google.maps.Map(document.getElementById("weekly_map_"+"#{item}"),
myOptions);
maps.push(weekly_map);
//set current location marker
for (var m = 0; m < maps.length; m++ ){
var location_marker = new google.maps.Marker({
position: currentLocation,
map: maps[m]
});
for ( var gf in geofences ){
var geofenceOptions = {
strokeColor: '#FF0000',
strokeOpacity: 0.8,
strokeWeight: 2,
fillColor: '#FF0000',
fillOpacity: 0.35,
map: maps[m],
center: geofences[gf].center,
radius: geofences[gf].radius
};
geofenceCircle = new google.maps.Circle(geofenceOptions);
}
}
//daily/weekly location path
var dailyPathPoints = [];
var weeklyPathPoints = [];
for (var i = 0; i < data.locations.length; i++){
var latitude = data.locations[i].latitude;
var longitude = data.locations[i].longitude;
var point = new google.maps.LatLng(latitude, longitude);
//if timestamp within week, add to week array
dailyPathPoints.push(point);
//if timestamp within day, add to daily array
weeklyPathPoints.push(point);
}
var dailyPath = new google.maps.Polyline({
path: dailyPathPoints,
geodesic: true,
strokeColor: '#009ED9',
strokeOpacity: 1.0,
strokeWeight: 2
});
dailyPath.setMap(daily_map);
var weeklyPath = new google.maps.Polyline({
path: dailyPathPoints,
geodesic: true,
strokeColor: '#009ED9',
strokeOpacity: 1.0,
strokeWeight: 2
});
weeklyPath.setMap(weekly_map);
$('a[data-toggle="tab"]').on('shown.bs.tab', function (e) {
google.maps.event.trigger(current_map, 'resize');
google.maps.event.trigger(daily_map, 'resize');
google.maps.event.trigger(weekly_map, 'resize');
current_map.setCenter(currentLocation);
daily_map.setCenter(currentLocation);
weekly_map.setCenter(currentLocation);
});
}); //$.get()
};
window.onload = loadMap;
.row
.col-md-4.col-sm-6.col-xs-12
.panel.with-nav-tabs.panel-default
.panel-heading#with-tabs
.navbar-text Location
ul.nav.nav-tabs
li.active
a(href='#tab1loc', data-toggle='tab') Current
li
a(href='#tab2loc', data-toggle='tab') Daily
li
a(href='#tab3loc', data-toggle='tab') Weekly
.panel-body
.tab-content
#tab1loc.tab-pane.fade.in.active
div(id='current_map_'+item, class='map')
#tab2loc.tab-pane.fade
div(id='daily_map_'+item, class='map')
#tab3loc.tab-pane.fade
div(id='weekly_map_'+item, class='map')
答案 1 :(得分:0)
使用替换为regEx和split函数的混合可以完成工作。
var str = "file:///K:/CKaing_C20_A01_Casino2/game.html?
First+Name=Testfirst&Last+Name=Testlast
&pnum=123-456-7890&postCode=A1A+1A1&startMoney=5000";
var argStrIndex = str.indexOf("?");
var argStr = str.substring(argStrIndex+1);
var args = argStr.replace(/\+/g," ").split("&");
for (var i=0;i<args.length;i++){
alert(args[i]);
}
答案 2 :(得分:0)
类似的东西:
var search = location.search
.replace(/^\?/,'')
.replace(/\+/g,' ')
.split('&')
.map(function(string){
var split = string.split('=');
var res={};
res[split[0]]=split[1];
return res;
});
应该返回
[{“First Name”:“Testfirst”},{“姓氏”:“Testlast”},{“pnum”:“123-456-7890”},{“postCode”:“A1A 1A1”} ,{ “startMoney”: “5000”}]“
你需要处理网址编码。
答案 3 :(得分:0)
已经给出了两个答案的组合(jsfiddle:http://jsfiddle.net/russianator/GymEq/)
var url = 'file:///K:/CKaing_C20_A01_Casino2/game.html?First+Name=Testfirst&Last+Name=Testlast&pnum=123-456-7890&postCode=A1A+1A1&startMoney=5000';
queryObject = {};
url.substring(url.indexOf('?')+1)
.replace(/\+/g,' ')
.split('&')
.forEach(function(item) {
splitItem = item.split('=');
queryObject[splitItem[0]] = splitItem[1];
});
返回如下对象:
{
"First Name": "Testfirst",
"Last Name": "Testlast",
...
}