一:Js的Url中傳遞中文參數亂碼問題,重點:encodeURI編碼,decodeURI解碼:

來源:互聯網
上載者:User

一:Js的Url中傳遞中文參數亂碼問題,重點:encodeURI編碼,decodeURI解碼:

1.傳參頁面
Javascript代碼:<script type=”text/javascript”>// <![CDATA[
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>
二:如何擷取Url“?”後,“=”的參數值:

A.首先用window.location.href擷取到全部url值。
B.用split截取“?”後的全部
C.split(“?”)後面的[1]內數字,預設從0開始計算

三:Js中escape,unescape,encodeURI,encodeURIComponent區別:

1.傳遞參數時候使用,encodeURIComponent否則url中很容易被”#”,”?”,”&”等敏感符號隔斷。
2.url跳轉時候使用,編碼用encodeURI,解碼用decodeURI。
3.escape() 只是為0-255以外 ASCII字元 做轉換工作,轉換成的 %u**** 這樣的碼,如果要用更多的字元如 UTF-8字元庫
就一定要用 encodeURIComponent() 或 encodeURI() 轉換才可以成 %nn%nn
這的碼才可以,其它情況下escape,encodeURI,encodeURIComponent編碼結果相同,所以為了全球的統一化進程,在用
encodeURIComponent() 或 encodeURI() 代替 escape() 使用吧!

可以任意轉載, 轉載時請務必以超連結形式標明文章原始出處及此聲明

本文地址: http://www.weiking.com.cn/post/654.html

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.