Show Output
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>Encode URI in JavaScript</title> </head> <body> <script> // URL unsafe string var item = "$url+/unsafe@item #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> </body> </html>