從A頁面通過url傳參到B頁面時,獲取URL中參數出現中文亂碼問題,解析url參數的正確方法如下,感興趣的朋友可以參考下
從A頁面通過url傳參到B頁面時,解析url參數可以用下面兩種方法: 方法一:正則分析法 代碼如下: 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; } 這樣調用: 代碼如下: alert(GetQueryString("參數名1")); alert(GetQueryString("參數名2")); alert(GetQueryString("參數名3")); 方法二: 代碼如下: <span style="font-size: 16px;"><Script language="javascript"> function GetRequest() { var url = location.search; //獲取url中"?"符後的字串 var theRequest = new Object(); if (url.indexOf("?") != -1) { var str = url.substr(1); strs = str.split("&"); for(var i = 0; i < strs.length; i ++) { theRequest[strs[i].split("=")[0]]=unescape(strs[i].split("=")[1]); } } return theRequest; } </Script></span> 這樣調用: 代碼如下: <Script language="javascript"> var Request = new Object(); Request = GetRequest(); var 參數1,參數2,參數3,參數N; 參數1 = Request['參數1']; 參數2 = Request['參數2']; 參數3 = Request['參數3']; 參數N = Request['參數N']; </Script> 如果參數中含有中文字符,注意轉編碼和解碼: 代碼如下: <span style="font-size:18px;">1.傳參頁面 Javascript代碼:<script type=”text/javascript”> function send(){ var url = "test01.html"; var userName = $("#userName").html(); window.open(encodeURI(url + "?userName=" + userName)); } </script> 2. 接收參數頁面:test02.html <script> var urlinfo = window.location.href;//獲取url var userName = urlinfo.split(“?”)[1].split(“=”)[1];//拆分url得到”=”後面的參數 $(“#userName”).html(decodeURI(userName)); </script></span>