URL:修订间差异
来自康健生活
| (未显示同一用户的4个中间版本) | |||
| 第15行: | 第15行: | ||
假定 URL 为 <code>https://kangkk.com/404?a=2</code>。 | 假定 URL 为 <code>https://kangkk.com/404?a=2</code>。 | ||
{{ | {{Double-columns|<syntaxhighlight lang="javascript"> | ||
const getQuery = name => { | const getQuery = name => { | ||
const reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i"); | const reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i"); | ||
| 第24行: | 第24行: | ||
console.log(getQuery('a'));</syntaxhighlight>|{{consoleout|<pre> | console.log(getQuery('a'));</syntaxhighlight>|{{consoleout|<pre> | ||
2 | 2 | ||
</pre>}}}} | |||
另一种处理方式。 | |||
{{Double-columns|<syntaxhighlight lang="javascript"> | |||
const getQuery = name => { | |||
const params = window.location.search.substring(1).split("&"); | |||
for (let i = 0; i < params.length; i++) { | |||
const pair = params[i].split("="); | |||
if(pair[0] == name) return pair[1]; | |||
} | |||
return void 0; | |||
} | |||
console.log(getQuery('a'));</syntaxhighlight>|{{consoleout|<pre> | |||
2 | |||
</pre>}}}} | |||
=== URLSearchParams === | |||
{{Double-columns|<syntaxhighlight lang="javascript"> | |||
let URL = "http://kangkk.com?a=2" | |||
const getQuery = url => { | |||
const urlSearchParams = new URLSearchParams(url.split('?')[1]); | |||
return Object.fromEntries(urlSearchParams.entries()); | |||
} | |||
console.log(getQuery(URL));</syntaxhighlight>|{{consoleout|<pre>{ a: '2' } | |||
</pre>}}}} | </pre>}}}} | ||
| 第31行: | 第56行: | ||
=== 附件 === | === 附件 === | ||
* [[文件:Rfc1738.txt|无|Rfc1738.txt]] | * [[文件:Rfc1738.txt|无|Rfc1738.txt]] | ||
* [[媒体:Rfc1738.txt|无]] | * [[媒体:Rfc1738.txt|无]] | ||
2026年11月26日 (四) 05:02的最新版本
简介
网络标准RFC 1738
URL长度
一般来说,URL只能使用英文字母、阿拉伯数字和某些标点符号,不能使用其他文字和符号。比如,世界上有英文字母的网址 “http://www.abc.com”,但是没有希腊字母的网址“http://www.aβγ.com”(读作阿尔法-贝塔-伽玛.com)。这是 因为 网络标准RFC 1738 做了硬性规定: "...Only alphanumerics [0-9a-zA-Z], the special characters "$-_.+!*'()," [not including the quotes - ed], and reserved characters used for their reserved purposes may be used unencoded within a URL." “只有字母和数字[0-9a-zA-Z]、一些特殊符号“$-_.+!*'(),”[不包括双引号]、以及某些保留字,才可以不经过编码直接用于 URL。” 这意味着,如果URL中有汉字,就必须编码后使用。但是麻烦的是,RFC 1738没有规定具体的编码方法,而是交给应用程序(浏览器)自己决定。这导致“URL编码”成为了一个混乱的领域。 下面就让我们看看,“URL编码”到底有多混乱。我会依次分析四种不同的情况,在每一种情况中,浏览器的URL编码方法都不一样。把它们的差异解释 清楚之后,我再说如何用JavaScript找到一个统一的编码方法。
参数
获取参数
假定 URL 为 https://kangkk.com/404?a=2。
const getQuery = name => {
const reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
const r = window.location.search.substr(1).match(reg);
if (r != null) return decodeURIComponent(r[2]);
return null;
}
console.log(getQuery('a'));
2
另一种处理方式。
const getQuery = name => {
const params = window.location.search.substring(1).split("&");
for (let i = 0; i < params.length; i++) {
const pair = params[i].split("=");
if(pair[0] == name) return pair[1];
}
return void 0;
}
console.log(getQuery('a'));
2
URLSearchParams
let URL = "http://kangkk.com?a=2"
const getQuery = url => {
const urlSearchParams = new URLSearchParams(url.split('?')[1]);
return Object.fromEntries(urlSearchParams.entries());
}
console.log(getQuery(URL));
{ a: '2' }
参考
http://blog.csdn.net/lvxiangan/article/details/8151670
附件
Rfc1738.txt Rfc1738.txt - 无