Skip to content

[javascript] 取網址 get 的 值(key-value),get query key/value

Published: at 02:29 PM

紀錄一下 vanilla.js 取網址 get  key-value 的方法


[javascript]

function getQueryStringValue (key) { return decodeURIComponent(window.location.search.replace(new RegExp(”^(?:.*[&\\?]” + encodeURIComponent(key).replace(/[\.\+\*]/g, ”\\$&”) + ”(?:\\=([^&]*))?)?.*$”, “i”), “$1”)); }

[/javascript]

參考資料:

https://stackoverflow.com/questions/9870512/how-to-obtaining-the-querystring-from-the-current-url-with-javascript