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 Overflow
🌐
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).
🌐
MDN 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).
🌐
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.
🌐
URL Encode
urlencoder.org β€Ί enc β€Ί javascript
URL Encoding of "javascript" - Online
Encode javascript to URL-encoded format with various advanced options. Our site has an easy to use online tool to convert your data.
🌐
freeCodeCamp
freecodecamp.org β€Ί news β€Ί javascript-url-encode-example-how-to-use-encodeuricomponent-and-encodeuri
JavaScript URL Encode Example – How to Use ...
August 4, 2020 - Need to accept query string parameters ... are used to encode Uniform Resource Identifiers (URIs) by replacing certain characters by one, two, three or four escape sequences representing the UTF-8 encoding of the character...
Find elsewhere
🌐
Master
master.dev β€Ί blog β€Ί encoding-and-decoding-urls-in-javascript
Encoding and Decoding URLs in JavaScript – Master.dev Blog
For example, because URLs cannot ... encoded as β€œ&”. JavaScript provides two functions for encoding URLs: encodeURI() and encodeURIComponent()....
🌐
DEV Community
dev.to β€Ί lico β€Ί understanding-how-spaces-are-encoded-20-with-encodeuri-vs-with-url-2d6c
Why Spaces Are Encoded: with encodeURI and +(plus) with URL / Differences Between encodeURI and URL - DEV Community
November 28, 2024 - While I couldn't find the specification in RFC for this behavior, MDN's encodeURIComponent documentation states: For application/x-www-form-urlencoded, spaces are to be replaced by +, so one may wish to follow a encodeURIComponent() replacement with an additional replacement of with +.
🌐
Latenode
community.latenode.com β€Ί other questions β€Ί javascript
How to Encode a URL in JavaScript? - JavaScript - Latenode Official Community
December 3, 2024 - What is the proper method to encode a URL in JavaScript to ensure it is safe for inclusion in a GET request? For example, I have a URL that looks like this: var siteUrl = β€˜http://example.com/index.html?key=1&anotherKey=2’…
🌐
Medium
ashishdev48.medium.com β€Ί encode-and-decode-uri-in-javascript-a-complete-guide-976f4986f6cf
Encode and Decode URI in JavaScript: A Complete Guide | by Ashishkumarjena | Medium
June 17, 2026 - The encodeURI() function is used to encode an entire URI. It’s ideal when you want to encode a full URL but want to preserve certain characters that have special meanings in URLs (such as ?, &, #, =).
🌐
URL Encode
urlencoder.org
URL Encode and Decode - Online
Encode to URL-encoded format or decode from it with various advanced options. Our site has an easy to use online tool to convert your data.
🌐
Base64 Encode
base64encode.org
Base64 Encode and Decode - Online
Enable this option to encode into an URL- and filename- friendly Base64 variant (RFC 4648 / Base64URL) where the "+" and "/" characters are respectively replaced by "-" and "_", as well as the padding "=" signs are omitted. Live mode: When you turn on this option the entered data is encoded immediately with your browser's built-in JavaScript functions, without sending any information to our servers.
🌐
Net Informations
net-informations.com β€Ί js β€Ί progs β€Ί encode.htm
How to encode a URL using JavaScript?
You can use the built-in method encodeURIComponent() to encode a URL in JavaScript.
🌐
MDN Web Docs
developer.mozilla.org β€Ί en-US β€Ί docs β€Ί Web β€Ί API β€Ί Fetch_API β€Ί Using_Fetch
Using the Fetch API - Web APIs | MDN
Other objects are converted to strings using their toString() method. For example, you can use a URLSearchParams object to encode form data (see setting headers for more information):
🌐
FFmpeg
ffmpeg.org
FFmpeg
Currently, the only codecs supported are: FFv1 (encoding and decoding) and ProRes RAW (decode only). ProRes (encode+decode) and VC-2 (encode+decode) implementations are complete and currently in review, to be merged soon and available with the next minor release.
🌐
Obfuscator
obfuscator.io
JavaScript obfuscator tool - Protect Your JS Code
VM (Virtual Machine) obfuscation transforms your JavaScript code into custom bytecode that runs on an embedded interpreter. Unlike standard obfuscation which still produces readable JavaScript, VM obfuscation completely hides your original code structure. Static analysis tools cannot understand the logic without first reverse-engineering the entire virtual machine.