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.
Asked
Active
Viewed 7,684 times
5

Machavity
- 30,841
- 27
- 92
- 100

ndisdabest
- 319
- 10
- 19
3 Answers
10
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;
}
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 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"
// }

Community
- 1
- 1

Ruan Mendes
- 90,375
- 31
- 153
- 217
-
"That is google's first result when searching for "dojo query string parameters" - ooft – Lawrence Tierney Jul 04 '13 at 12:28
-
you have defined the URI itself in dojo , can you please tell if Dojo has a method to call it from browser URL, or I need to do simple javascript – Friendy Sep 29 '15 at 10:43
-
1@friendy you can pass in `location.search` – Ruan Mendes Sep 29 '15 at 11:06
-
@lawrencetierney Removed that "not so friendly" bit :-) – Ruan Mendes Sep 29 '15 at 11:10
1
In new dojo it's accessed with io-query:
require([
"dojo/io-query",
], function (ioQuery) {
GET = ioQuery.queryToObject(decodeURIComponent(dojo.doc.location.search.slice(1)));
console.log(GET.id);
});

Pehmolelu
- 3,534
- 2
- 26
- 31
0
Since dojo 0.9, there is a better option, queryToObject.
dojo.queryToObject(query)
See this similar question with what I think is a cleaner answer.