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 › 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 ·
🌐
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.
Find elsewhere
🌐
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.
🌐
CodeBasics
code-basics.com › programming › javascript course › encoding
Encoding | JavaScript | CodeBasics
Different encodings have different numbers of characters. At first, small tables like ASCII were enough for programmers.
🌐
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 - This guide will walk you through the primary URI encoding/decoding functions in JavaScript and the use cases for each.
🌐
JavaScript in Plain English
javascript.plainenglish.io › javascript-how-to-encode-urls-d87ccfb46505
JavaScript — How To Encode URLs
April 13, 2024 - The encodeURIComponent() function ... that encodes a URI (Uniform Resource Identifier) by replacing each instance of certain characters with one, two, three, or four escape sequences representing the UTF-8 encoding of the character...
🌐
Microsoft
devblogs.microsoft.com › dev blogs › .net blog › writing node.js addons with .net native aot
Writing Node.js addons with .NET Native AOT - .NET Blog
April 20, 2026 - Returning a string to JavaScript is the same process in reverse. We encode a .NET string into a UTF-8 buffer and pass it to napi_create_string_utf8:
🌐
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?
🌐
Codú
codu.co › home › feed › niall maher › how to encode a url in javascript
How to Encode a URL in JavaScript | by Niall Maher | Codú
June 3, 2024 - JavaScript provides a built-in function called encodeURIComponent() to encode a URL.