Hàm encodeURIComponent() mã hóa một thành phần URI (component) bằng cách thay thế tất cả các ký tự đặc biệt (kể cả các ký tự dành riêng như / ? : @ & = + $ #) bằng các dãy thoát %XX. Dùng cho tham số query string, path segment.
Cú pháp
Cú pháp của nó như sau:
encodeURIComponent(uriComponent)
Ví dụ
<html> <head> <title>Ví dụ JavaScript</title> </head> <body> <script type="text/javascript"> var result1 = encodeURIComponent('https://www.qries.com/'); document.write(result1); document.write("<br>"); var result2 = encodeURIComponent('https://www.tutorialspoint.com/'); document.write(encodeURIComponent(result2)); </script> </body> </html>
Kết quả
http%3A%2F%2Fwww.qries.com%2F http%253A%252F%252Fwww.tutorialspoint.com%252F
Lưu ý: encodeURIComponent() mã hóa nhiều ký tự hơn encodeURI(). Chỉ không mã hóa: A-Z a-z 0-9 - _ . ! ~ * ' ( )