URL:修订间差异
来自康健生活
| (未显示同一用户的2个中间版本) | |||
| 第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"); | ||
| 第28行: | 第28行: | ||
另一种处理方式。 | 另一种处理方式。 | ||
{{ | {{Double-columns|<syntaxhighlight lang="javascript"> | ||
const getQuery = name => { | const getQuery = name => { | ||
const params = window.location.search.substring(1).split("&"); | const params = window.location.search.substring(1).split("&"); | ||
| 第39行: | 第39行: | ||
console.log(getQuery('a'));</syntaxhighlight>|{{consoleout|<pre> | console.log(getQuery('a'));</syntaxhighlight>|{{consoleout|<pre> | ||
2 | 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>}}}} | ||
| 第46行: | 第56行: | ||
=== 附件 === | === 附件 === | ||
* [[文件:Rfc1738.txt|无|Rfc1738.txt]] | * [[文件:Rfc1738.txt|无|Rfc1738.txt]] | ||
* [[媒体:Rfc1738.txt|无]] | * [[媒体:Rfc1738.txt|无]] | ||