jquery對url中的中文解碼


項目中要實現一個select選擇器選擇后跳轉url,並保存selected的值。

url是用get來傳遞參數,所以考慮加載新頁面時,讀取參數值,並賦值到select中。

但是由於url的參數使用的是中文,select不識別,所以通過jquery現成的轉碼函數,一句話搞定~!

 

select選中值的防刷新:
每次加載頁面后讀取url中的參數值,然后設定select的選中值,由於url中包含中文,使用了jquery的解碼函數,

var myurl=new LG.URL(window.location.href);//js封裝的url操作函數
$("#yewu").val(decodeURIComponent(myurl.get("yewu")));//jquery解碼函數

 

Encode URL String

<script>
var url = $(location).attr('href'); //get current url
//OR
var url = 'folder/index.html?param=#23dd&amp;noob=yes'; //or specify one

var encodedUrl = encodeURIComponent(url);
console.log(encodedUrl);

//outputs folder%2Findex.html%3Fparam%3D%2323dd%26noob%3Dyes
</script>


Decode URL String

<script>
var url = $(location).attr('href'); //get current url
//OR
var url = 'folder%2Findex.html%3Fparam%3D%2323dd%26noob%3Dyes'; //or specify one

var decodedUrl = decodeURIComponent(url);
console.log(decodedUrl);
//outputs folder/index.html?param=#23dd&amp;noob=yes
</script>

 


免責聲明!

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



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