js截取get參數亂碼問題之解決


舉個例子說一下:

http://wwww.yctech.com/blog/post?id=1

像這樣的話,通常通過如下代碼直接截取不用做任何處理:

function getQueryString(name) {
    var result = window.location.search.match(new RegExp("[\?\&]" + name + "=([^\&]+)", "i"));
    if (result == null || result.length < 1) {
        return "";
    }
    return result[1];
}

但是當http://wwww.yctech.com/blog/post?id=1 變成http://wwww.yctech.com/blog/post?id=挑戰者

這時,如果用getQueryString(name)方法截取的話,那么就會出現亂碼,對於這種亂碼的解決方式也很簡單:
就是通過encodeURI()解決。

如:

var ids = getQueryString("id");//亂碼
var id = encodeURL(ids);//處理亂碼
alert(id);//彈出挑戰者

參考資料如下:
js傳url中文參數亂碼問題

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM