使用Dojo获取Querystring [英] Get Querystring with Dojo

查看:117
本文介绍了使用Dojo获取Querystring的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

全部,

用户将击中一个包含一个名为querytype的querystring的URL。由于很多原因,我需要使用javascript(Dojo)读取这个querystring并将其值保存到变量中。我已经做了大量的研究,试图找到如何做到这一点,我已经发现了一些可能性,但是没有一个似乎实际上读入一个没有硬编码在脚本中的一个查询字符串。

Users will be hitting up against a URL that contains a querystring called "inquirytype". For a number of reasons, I need to read in this querystring 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 querystring that isn't hardcoded somewhere in the script.

你知道的任何例子或想法可以帮助我吗?

Any examples or ideas you know of that would help me here?

谢谢!

推荐答案

您可以使用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中提取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获取Querystring的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆