encodeURIComponent()
MethodYou can use the built-in method encodeURIComponent()
to safely encode a URL in JavaScript.
The encodeURIComponent()
method encode all characters except the following:
A-Z a-z 0-9 - _ . ! ~ * ' ( )
Let's check out an example to understand how this method basically works:
<script>
// URL unsafe string
var item = "$url+/[email protected] #name;"
// Encode item name and append to URL
var url = "http://example.com/search.php?product=" + encodeURIComponent(item);
document.write(url);
// Prints: http://example.com/search.php?product=%24url%2B%2Funsafe%40item%20%23name%3B
</script>
You can alternatively use the encodeURI()
method, which does not encode characters that have special meaning (reserved characters) for a URI e.g. #
, /
, ?
, etc.
The encodeURI()
method encode all characters except the following:
A-Z a-z 0-9 ; , / ? : @ & = + $ - _ . ! ~ * ' ( ) #
The following example demonstrates how this method actually works:
<script>
// Sample URL
var url = "http://example.com/search.php?product=laptop&price=500#featured";
document.write(url);
// Prints: http://example.com/search.php?product=laptop&price=500#featured
</script>
Hi, My name is Harsukh Makwana. i have been work with many programming language like php, python, javascript, node, react, anguler, etc.. since last 5 year. if you have any issue or want me hire then contact me on [email protected]
Laravel 8 - Excel and CSV Import Export to Database using maatwebsite/excel with Example.
Excel file import-export to the database...Laravel Eloquent Many to Many Polymorphic Relationship Tutorial with Example
Laravel provides eloquent relationship w...How to check whether a variable is set or not in PHP
Use the PHP isset() function...How to use Basic Authorization in PHP curl
When accessing API request over PHP curl...How to Get the Current Date and Time in PHP
Use the PHP date() Function You can s...