One of the projects I’ve been working on recently was a requirement to get URL parameters on the page.
I found the following method by searching the Internet
function getQueryString(name) { var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i"); var r = window.location.search.substr(1).match(reg); if (r ! = null) return unescape(r[2]); return null; } But when there are Chinese characters in the parameter, the problem of garbled characters will appear. It turns out that the browser uses encodeURI to encode Chinese characters by default, so it needs to use decodeURI instead of UNescape when decoding. After a little modification of the code above, the problem of Chinese garbled characters can be solvedCopy the code
The modified code:
function getQueryString(name) { var reg = new RegExp(“(^|&)” + name + “=([^&]*)(&|$)”, “i”); var r = window.location.search.substr(1).match(reg); if (r ! = null) return decodeURI(r[2]); return null; }
Original quote:
Blog.csdn.net/u010485134/…