Check out the built-in function encodeURIComponent(str) and encodeURI(str).
In your case, this should work:
var myOtherUrl =
"http://example.com/index.html?url=" + encodeURIComponent(myUrl);
Answer from Buu on Stack OverflowMDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › encodeURI
encodeURI() - JavaScript | MDN
The encodeURI() function encodes a URI by replacing each instance of certain characters by one, two, three, or four escape sequences representing the UTF-8 encoding of the character (will only be four escape sequences for characters composed of two surrogate characters).
W3Schools
w3schools.com › jsref › jsref_encodeuri.asp
W3Schools.com
new Error() cause isError() name message JS Global · decodeURI() decodeURIComponent() encodeURI() encodeURIComponent() escape() eval() Infinity isFinite() isNaN() NaN Number() parseFloat() parseInt() String() undefined unescape() JS Intl Namespace JS Iterators ·
11:50
Encoding Text in HTML with Javascript - Learn to Code - Ep 5 - YouTube
10:18
JavaScript How to Encode URL - YouTube
02:45
JavaScript Base64 Encode and Decode - btoa() and atob() - YouTube
00:53
️ EncodeURI & DecodeURI in JavaScript
02:03
How to Encode a URL in JavaScript (encodeURI vs encodeURIComponent) ...
04:25
Convert Image/Blob to Base64 in JavaScript | Blob to String Made Easy!
Top answer 1 of 16
3239
Check out the built-in function encodeURIComponent(str) and encodeURI(str).
In your case, this should work:
var myOtherUrl =
"http://example.com/index.html?url=" + encodeURIComponent(myUrl);
2 of 16
1710
You have three options:
escape()will not encode:@*/+encodeURI()will not encode:~!@#$&*()=:/,;?+'encodeURIComponent()will not encode:~!*()'
But in your case, if you want to pass a URL into a GET parameter of other page, you should use escape or encodeURIComponent, but not encodeURI.
See Stack Overflow question Best practice: escape, or encodeURI / encodeURIComponent for further discussion.
Honeybadger
honeybadger.io › blog › encode-javascript
The character encoding cheat sheet for JS developers - Honeybadger Developer Blog
September 21, 2023 - In this article, we've covered the basics of character encoding in JavaScript, including the different encoding standards, how they work, and how to work with them in Node.js and web browsers. We've also covered some best practices for working with character encoding in JavaScript and provided tips and techniques for debugging encoding issues.
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › encodeURIComponent
encodeURIComponent() - JavaScript | MDN
October 30, 2025 - The encodeURIComponent() function encodes a URI by replacing each instance of certain characters by one, two, three, or four escape sequences representing the UTF-8 encoding of the character (will only be four escape sequences for characters composed of two surrogate characters).
DigitalOcean
digitalocean.com › community › tutorials › how-to-encode-and-decode-strings-with-base64-in-javascript
How To Encode and Decode Strings with Base64 in JavaScript | DigitalOcean
October 2, 2025 - This article explains how to use btoa() to encode strings into Base64 and atob() to decode them back. We will also cover common applications for Base64 in web development and address the limitations of these functions, particularly when working with Unicode characters, providing a more robust solution for modern use cases. The tutorial also provides a method for handling non-ASCII text and explains the correct approach for Node.js ...
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › API › TextEncoder › encode
TextEncoder: encode() method - Web APIs | MDN
The TextEncoder.encode() method takes a string as input, and returns a Uint8Array containing the string encoded using UTF-8. js · encode(string) string · A string containing the text to encode. A Uint8Array object containing the UTF-8 encoding of the input string.
Eric Meyer
meyerweb.com › eric › tools › dencoder
URL Decoder/Encoder
The URL Decoder/Encoder is licensed under a Creative Commons Attribution-ShareAlike 2.0 License.
PortSwigger
portswigger.net › bappstore › 1da8a127327d42eabff3725efe93db86
JavaScript Encode As - PortSwigger
This extension allows you to encode strings in the JavaScript format (i.e. Unicode/Hex), similar to Burp's Convert-To but specifically for within JSON/JavaScript.
URL Encode
urlencoder.org
URL Encode and Decode - Online
For the files section, this is partially irrelevant since files already contain the corresponding separators, but you can define which one to use for the "encode each line separately" and "split lines into chunks" functions.
Base64.Guru
base64.guru › home › base64 converter › base64 encode
Image to Base64 | Base64 Encode | Base64 Converter | Base64
Sometimes you have to send or output an image within a text document (for example, HTML, CSS, JSON, XML), but you cannot do this because binary characters will damage the syntax of the text document. To prevent this, for example, you can encode image to Base64 and embed it using the data URI.
GitHub
gist.github.com › 1214016
URL Encode and Decode in JavaScript · GitHub
Clone this repository at <script src="https://gist.github.com/jarrodbell/1214016.js"></script> Save jarrodbell/1214016 to your computer and use it in GitHub Desktop. ... This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters ... Why not use Javascript's encodeURIComponent() and decodeURIComponent() functions for URL encoding and decoding?