使用Dojo获取查询字符串 [英] Get Query String with Dojo
问题描述
用户将使用包含名为查询类型
的查询字符串的URL。由于多种原因,我需要使用javascript(Dojo)读取此查询字符串并将其值保存到变量中。我已经做了很多研究,试图找到如何做到这一点的方法,但是我发现了几种可能性,但是似乎没有一种可能是在脚本中未进行硬编码的查询字符串中实际读取的。
Users will be hitting up against a URL that contains a query string called inquirytype
. For a number of reasons, I need to read in this query string with javascript (Dojo) and save its value to a variable. I've done a fair amount of research trying to find how to do this, and I've discovered a few possibilities, but none of them seem to actually read in a query string that isn't hard-coded somewhere in the script.
推荐答案
您可以使用location.search从URL中访问参数,而无需Dojo 可以通过手动url参数确定javascript属性值吗?
You can access parameters from the url using location.search without Dojo Can a javascript attribute value be determined by a manual url parameter?
function getUrlParams() {
var paramMap = {};
if (location.search.length == 0) {
return paramMap;
}
var parts = location.search.substring(1).split("&");
for (var i = 0; i < parts.length; i ++) {
var component = parts[i].split("=");
paramMap [decodeURIComponent(component[0])] = decodeURIComponent(component[1]);
}
return paramMap;
}
然后,您可以执行以下操作从URL <$ c $中提取ID c> /hello.php?id=5&name=value
Then you could do the following to extract id from the url /hello.php?id=5&name=value
var params = getUrlParams();
var id = params['id']; // or params.id
Dojo提供 http://dojotoolkit.org/reference-guide/dojo/queryToObject.html 比我的简单实现要聪明一点,它可以创建数组
Dojo provides http://dojotoolkit.org/reference-guide/dojo/queryToObject.html which is a bit smarter than my simple implementation and creates arrays out of duplicated keys.
var uri = "http://some.server.org/somecontext/?foo=bar&foo=bar2&bit=byte";
var query = uri.substring(uri.indexOf("?") + 1, uri.length);
var queryObject = dojo.queryToObject(query);
//The structure of queryObject will be:
// {
// foo: ["bar", "bar2],
// bit: "byte"
// }
这篇关于使用Dojo获取查询字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!