Sử dụng decodeURI()
Để giải mã URL đã được mã hóa bằng encodeURI(), dùng decodeURI().
Ví dụ hoàn chỉnh (mã hóa rồi giải mã)
<!DOCTYPE html>
<html>
<body>
<button onclick="display()">Check</button>
<p id="demo"></p>
<script>
function display() {
var uri = "welcome msg.jsp?name=\u00E5mit&sub=programming";
var encode = encodeURI(uri);
var decode = decodeURI(encode);
var result = "Encode= " + encode + "<br>" + "Decode= " + decode;
document.getElementById("demo").innerHTML = result;
}
</script>
</body>
</html>
Kết quả
Encode= welcome%20msg.jsp?name=%C3%A5mit&sub=programming Decode= welcome msg.jsp?name=åmit&sub=programming
Cặp hàm tương ứng
| Mã hóa | Giải mã | Phạm vi |
|---|---|---|
| encodeURI() | decodeURI() | URL đầy đủ |
| encodeURIComponent() | decodeURIComponent() | Thành phần (param) |