Getting a number out of the url
This question already has an answer here:
You can make use of split()
var url = 'http://localhost:17241/Chart.aspx?id=11'
var params = url.split('?');
var id=params[1].split('=')[1]; //params[1] will contain everything after ?
console.log(id);
EDIT
To get the url inside the var url
replace the first line with
var url = window.location.href;
It's called query string
here is the function,
function getParameterByName(name) {
name = name.replace(/[[]/, "[").replace(/[]]/, "]");
var regex = new RegExp("[?&]" + name + "=([^&#]*)"),
results = regex.exec(location.search);
return results === null ? "" : decodeURIComponent(results[1].replace(/+/g, " "));
}
and how you call
getParameterByName(name)
Above code is from here How can I get query string values in JavaScript?
If you want it as an actual number I would write a generic version
function getParamAsNumber(url, param) {
param = param + '=';
if (url.indexOf(param) !== -1) {
return parseInt(url.substr(url.indexOf(param) + param.length));
}
}
It converts to integer the string after param + '=' (in your case 'id=')
So you can do
getParamAsNumber(window.location.href, 'id');
链接地址: http://www.djcxy.com/p/17610.html
上一篇: 在Javascript中获取querystring数组值
下一篇: 从网址获取号码