简体   繁体   English

我应该使用什么 JavaScript 库来解析 URL 参数?

[英]What JavaScript library should I use for parsing URL parameters?

How do I parse URL parameters in JavaScript?如何在 JavaScript 中解析 URL 参数? (These are the parameters I would ordinarily call GET parameters or CGI parameters, but in this case the page is basically submitting to itself, not a server, so there is no GET request and definitely no CGI program.) (这些是我通常称为 GET 参数或 CGI 参数的参数,但在这种情况下,页面基本上是提交给自己,而不是服务器,因此没有 GET 请求,也绝对没有 CGI 程序。)

I've seen a number of routines on the net that I can copy, but I have no idea how robust any of them are.我在网上看到了很多可以复制的例程,但我不知道其中的任何一个有多健壮。 I'm used to other languages like Perl and Java where I can rely on an extremely well-tested and robust library that I know will handle millions of little edge-cases in the standard.我已经习惯了其他语言,例如 Perl 和 Java,在这些语言中,我可以依赖一个经过充分测试且功能强大的库,我知道它可以处理标准中的数百万个小边缘情况。 I would like the same here, rather than just cutting and pasting an example.我想在这里也一样,而不仅仅是剪切和粘贴示例。

Here's are two simple functions that do the job: http://adamv.com/dev/javascript/querystring这是完成这项工作的两个简单函数:http: //adamv.com/dev/javascript/querystring

Here is a sample of the API Reference:以下是 API 参考示例:

var qs = new Querystring();

// Parse a given querystring
var qs2 = new Querystring("name1=value1&name2=value2");

var v1 = qs2.get("name1");
var v3 = qs2.get("name3", "default value");

If it's "submitting to itself," do you need to do GET parameters?如果是“提交给自己”,还需要做GET参数吗?

But if you do, most browsers now have the decodeURIComponent function which will handle individual parameters;但是如果你这样做,大多数浏览器现在都有decodeURIComponent函数来处理单独的参数; your job is to split them on & ( String#split will do that nicely).你的工作是将它们拆分为 & ( String#split会很好地做到这一点)。 If you want a library, jQuery and Prototype are both well-used and tested.如果你想要一个库,jQuery 和 Prototype 都被很好地使用和测试过。

The best way I have found is to simply do it yourself and funnel the params into a global key/value object.我发现的最好方法是简单地自己做,并将参数汇集到一个全局键/值对象中。

Getting quer params is simple...获取 quer 参数很简单......

just take a couple of.split()'s只需要几个.split()的

var myquery = thewholeurl.split("?")[1]; //will get the whole querystring with the ?

then you can do a那么你可以做一个

myparams = myquery.split("&") myparams = myquery.split("&")

then you can do那么你可以做

for each param in myparams
{
temp = param.split("=");

mykeys.push(temp[0]);
myvalues.push(temp[1]);

OR

myObject[temp[0]] = temp[1];
}

It's just a matter of style.这只是风格问题。

This is not perfect code, just psuedo stuff to give you the idea.这不是完美的代码,只是伪造的东西给你的想法。

I found this useful for simple url parsing, modifying url (like adding new query params): https://github.com/derek-watson/jsUri我发现这对于简单的 url 解析、修改 url(比如添加新的查询参数)很有用: https ://github.com/derek-watson/jsUri

I use the parseUri library available here: http://stevenlevithan.com/demo/parseuri/js/我使用此处提供的 parseUri 库: http ://stevenlevithan.com/demo/parseuri/js/

It allows you to do exactly what you are asking for:它允许您完全按照您的要求进行操作:

var uri = 'http://google.com/?q=stackoverflow';
var q = uri.queryKey['q'];
// q = 'stackoverflow'

I've been using it for a while so far and haven't had any problems.到目前为止,我已经使用了一段时间,没有遇到任何问题。

I think this library would work quite well, it is independent so you can use it with JQuery or with YAHOO or Dojo, another advantage is that it is pretty well documented.我认为这个库会工作得很好,它是独立的,所以你可以将它与 JQuery 或 YAHOO 或 Dojo 一起使用,另一个优点是它有很好的文档记录。

http://www.openjsan.org/doc/t/th/theory/HTTP/Query/0.03/lib/HTTP/Query.html

You can use HTTP.Query to do all of the work for you in this case.在这种情况下,您可以使用 HTTP.Query 为您完成所有工作。 It is only like 1.2 KB compressed so you could even include it in a bigger library if you wanted.它压缩后只有 1.2 KB,因此您甚至可以根据需要将其包含在更大的库中。

I recommend query-string library我推荐query-string

Installing:安装:

npm install query-string

Usage:用法:

import queryString from 'query-string';

console.log(location.search);
//=> '?foo=bar'

const parsed = queryString.parse(location.search);
console.log(parsed);
//=> {foo: 'bar'}

parsed.foo = 'unicorn';
parsed.ilike = 'pizza';

const stringified = queryString.stringify(parsed);
//=> 'foo=unicorn&ilike=pizza'

location.search = stringified;
// note that `location.search` automatically prepends a question mark
console.log(location.search);
//=> '?foo=unicorn&ilike=pizza'

https://www.npmjs.com/package/query-string https://www.npmjs.com/package/query-string

Javascript has no built in support for URL parameters. Javascript 没有对 URL 参数的内置支持。

Anyway, the location.search property returns the portion of current page URL starting from the question mark ('?').不管怎样, location.search属性返回当前页面 URL 从问号('?')开始的部分。

From this, you can write your own parameter parser or you can make use of one of those available in most common Javascript frameworks, such as JQuery and similar.由此,您可以编写自己的参数解析器,也可以使用最常见的 Javascript 框架(例如 JQuery 等)中可用的参数解析器之一。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用带有参数的远程URL并在代码中使用响应? - what should I use to hit a remote url with parameters and use the response in code? 我应该使用哪个 javascript 库? - Which javascript library should I use? 我什么时候应该使用javascript框架库? - When should I use a javascript framework library? 我应该使用哪个JavaScript库创建具有可拖动元素的多媒体帖子? - What JavaScript library should I use for creating a multimedia post with draggable elements? 我应该在这个练习中使用 JavaScript 中的哪个函数? - What function in JavaScript should I use for this exercise? 我应该使用什么工作流程进行JavaScript编辑? - What workflow should I use for JavaScript editing? 我很困惑是否应该在后端使用 URL 路径参数或查询参数进行过滤? - I'm confused whether I should use URL path parameters or query parameters for filtering on the back-end? 今天(2011年)应该使用哪个JavaScript hashchange / history库? - Which JavaScript hashchange/history library should I use today (2011)? 我应该使用哪个JavaScript库进行客户端连字? - Which JavaScript library should I use for client-side hyphenation? 我应该为这样的javascript按钮使用什么标签? - What tag should i use for a javascript button like this?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM