考虑:
http://example.com/page.html?returnurl=%2Fadmin
对于page.html内的js,它如何检索GET参数?
对于上面的简单例子,func('returnurl')应该是/admin。
但它也应该适用于复杂的查询字符串…
考虑:
http://example.com/page.html?returnurl=%2Fadmin
对于page.html内的js,它如何检索GET参数?
对于上面的简单例子,func('returnurl')应该是/admin。
但它也应该适用于复杂的查询字符串…
当前回答
我的解决方案扩展了@tak3r的。
当没有查询参数时返回空对象,并支持数组符号?a=1&a=2&a=3:
function getQueryParams () {
function identity (e) { return e; }
function toKeyValue (params, param) {
var keyValue = param.split('=');
var key = keyValue[0], value = keyValue[1];
params[key] = params[key]?[value].concat(params[key]):value;
return params;
}
return decodeURIComponent(window.location.search).
replace(/^\?/, '').split('&').
filter(identity).
reduce(toKeyValue, {});
}
其他回答
我这样做(检索一个特定的get-parameter,这里'parameterName'):
var parameterValue = decodeURIComponent(window.location.search.match(/(\?|&)parameterName\=([^&]*)/)[2]);
Window.location.search将返回?上。下面的代码将删除?,使用split将键/值数组分开,然后将命名属性分配给params对象:
function getSearchParameters() {
var prmstr = window.location.search.substr(1);
return prmstr != null && prmstr != "" ? transformToAssocArray(prmstr) : {};
}
function transformToAssocArray( prmstr ) {
var params = {};
var prmarr = prmstr.split("&");
for ( var i = 0; i < prmarr.length; i++) {
var tmparr = prmarr[i].split("=");
params[tmparr[0]] = tmparr[1];
}
return params;
}
var params = getSearchParameters();
然后,您可以通过调用params.test从http://myurl.com/?test=1获取测试参数。
我创建了一个简单的JavaScript函数来从URL访问GET参数。
只要包含这个JavaScript源代码,就可以访问get参数。 例如:在http://example.com/index.php?language=french中,语言变量可以通过$_GET["language"]访问。类似地,所有参数的列表将作为数组存储在变量$_GET_Params中。JavaScript和HTML都在下面的代码片段中提供:
<!DOCTYPE html> <html> <body> <!-- This script is required --> <script> function $_GET() { // Get the Full href of the page e.g. http://www.google.com/files/script.php?v=1.8.7&country=india var href = window.location.href; // Get the protocol e.g. http var protocol = window.location.protocol + "//"; // Get the host name e.g. www.google.com var hostname = window.location.hostname; // Get the pathname e.g. /files/script.php var pathname = window.location.pathname; // Remove protocol part var queries = href.replace(protocol, ''); // Remove host part queries = queries.replace(hostname, ''); // Remove pathname part queries = queries.replace(pathname, ''); // Presently, what is left in the variable queries is : ?v=1.8.7&country=india // Perform query functions if present if (queries != "" && queries != "?") { // Remove question mark '?' queries = queries.slice(1); // Split all the different queries queries = queries.split("&"); // Get the number of queries var length = queries.length; // Declare global variables to store keys and elements $_GET_Params = new Array(); $_GET = {}; // Perform functions per query for (var i = 0; i < length; i++) { // Get the present query var key = queries[i]; // Split the query and the value key = key.split("="); // Assign value to the $_GET variable $_GET[key[0]] = [key[1]]; // Assign value to the $_GET_Params variable $_GET_Params[i] = key[0]; } } } // Execute the function $_GET(); </script> <h1>GET Parameters</h1> <h2>Try to insert some get parameter and access it through JavaScript</h2> </body> </html>
这个函数使用正则表达式,如果参数不存在或没有任何值,则返回null:
function getQuery(q) {
return (window.location.search.match(new RegExp('[?&]' + q + '=([^&]+)')) || [, null])[1];
}
您可以使用位置对象中可用的搜索函数。搜索函数提供URL的参数部分。详细信息可在位置对象中找到。
你将不得不解析结果字符串来获得变量和它们的值,例如在'='上拆分它们。