JS擷取url參數及url編碼、解碼

來源:互聯網
上載者:User

標籤:符號   二次   get   name   win   param   使用   部分   utf-8   

完整的URL由這幾個部分構成:scheme://host:port/path?query#fragment ,各部分的取法如下:

 

window.location.href:擷取完整url的方法:,即scheme://host:port/path?query#fragment

window.location.protocol:擷取rul協議scheme

window.location.host:擷取host

window.location.port:擷取連接埠號碼

window.location.pathname:擷取url路徑

window.location.search:擷取參數query部分,注意此處返回的是?query

window.location.hash:擷取錨點,#fragment

 

 

在js中可以使用escape(), encodeURL(), encodeURIComponent(),三種方法都有一些不會被編碼的符號:

 

escape():@ * / +

 

encodeURL():! @ # $& * ( ) = : / ; ? + ‘


encodeURIComponent():! * ( ) ‘

 

在java端可以使用URLDecoder.decode(“中文”, "UTF-8");來進行解碼

 

但是由於使用request.getParameter()來擷取參數時已經對編碼進行了一次解碼,所以一般情況下只要在js中使用


encodeURIComponent("中文");

 

在java端直接使用request.getParameter()來擷取即可返回中文。

 

如果你想在java端使用URLDecoder.decode(“中文”, "UTF-8");來解碼也可以在js中進行二次編碼,即:

 

encodeURIComponent(encodeURIComponent("中文"));

 

如果不進行二次編碼的話,在java端通過decode方法取的會是亂碼。

JS擷取url參數及url編碼、解碼

JS擷取url參數及url編碼、解碼

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.