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
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 › API › TextEncoder › encode
TextEncoder: encode() method - Web APIs | MDN
June 28, 2025 - const sourcePara = document.querySelector(".source"); const resultPara = document.querySelector(".result"); const string = sourcePara.textContent; const textEncoder = new TextEncoder(); const encoded = textEncoder.encode(string); resultPara.textContent = `${resultPara.textContent} ${encoded}`;
Discussions

How to encode a string in JavaScript for displaying in HTML? - Stack Overflow
Possible Duplicate: How to escape HTML How can a string be converted to HTML in JavaScript? e.g. var unsafestring = " "; var safestring = magic(unsafestring); where More on stackoverflow.com
🌐 stackoverflow.com
How can I json encode this string "[{"First":"Tom","Last":"Bowsen"}]"
That's already encoded. What you want to do is decode it. import 'dart:convert'; void main(){ final content = '[{"First":"Tom","Last":"Bowsen"}]'; final map = json.decode(content)[0] as Map; print(map['First']); print(map['Last']); } More on reddit.com
🌐 r/dartlang
6
5
December 29, 2020
[Javascript] Console escape character for a new line "\n" doesn't seem to work in Node

Use console.log.

More on reddit.com
🌐 r/learnprogramming
10
1
February 7, 2018
How do I put a JSON object in a URL as a GET parameter?

JSON itself is a string. To turn your object into JSON you simply need to call a JSON.stringify(obj). Make sure you url-encode the string as well with encodeURI() before appending it to a link

More on reddit.com
🌐 r/javascript
25
13
December 25, 2017
🌐
TutorialsPoint
tutorialspoint.com › article › how-to-encode-a-string-in-javascript
How to encode a string in JavaScript?
The btoa() method encodes a string into Base64 format, which is commonly used for data transmission and storage.
Find elsewhere
🌐
web.dev
web.dev › articles › the nuances of base64 encoding strings in javascript
The nuances of base64 encoding strings in JavaScript | Articles | web.dev
October 17, 2023 - Importantly, JavaScript processes strings as UTF-16. This breaks functions like btoa(), which effectively operate on the assumption that each character in the string maps to a single byte. This is stated explicitly on MDN: The btoa() method creates a Base64-encoded ASCII string from a binary string (i.e., a string in which each character in the string is treated as a byte of binary data).
🌐
Honeybadger
honeybadger.io › blog › encode-javascript
The character encoding cheat sheet for JS developers - Honeybadger Developer Blog
September 21, 2023 - However, JavaScript can be used to dynamically load or generate content on a webpage, such as text or images. In these cases, it may be necessary to specify the type of character encoding for the dynamically generated content, which can’t be done with a meta tag. const string = 'こんにちは、世界!'; // Japanese string const encodedString = encodeURIComponent(string); console.log(encodedString);
🌐
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).
🌐
Coderstoolbox
coderstoolbox.net › string
Online JavaScript string encoder .. ...
Encode and decode strings: Base64, URL, XML, JavaScript
🌐
GeeksforGeeks
geeksforgeeks.org › javascript › javascript-encode-decode-a-string-to-base64
JavaScript | Encode/Decode a string to Base64 - GeeksforGeeks
May 28, 2024 - These functions help in encoding special characters in a URL or decoding encoded strings back to their original form. This method encodes a string in base-64 and uses the "A-Z", "a-z", "0-9", "+", "/" and "=" characters to encode the provided string.
🌐
PortSwigger
portswigger.net › web-security › cross-site-scripting
What is cross-site scripting (XSS) and how to prevent it? | Web Security Academy
Escape your outputs with htmlentities and ENT_QUOTES for HTML contexts, or JavaScript Unicode escapes for JavaScript contexts. How do I prevent XSS in Java? Filter your inputs with a whitelist of allowed characters and use a library such as Google Guava to HTML-encode your output for HTML contexts, or use JavaScript Unicode escapes for JavaScript contexts.
🌐
Token
token.dev
JWT Debugger
Encode or Decode JWTs · Paste a JWT and decode its header, payload, and signature, or provide header, payload, and signature information to generate a JWT. Learn More · Algorithm · JWT String · Warning: Security Tokens should be kept secret. Verification of the JWT is done in the browser only!
🌐
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 - We will also cover common applications ... explains the correct approach for Node.js environments. ... JavaScript’s btoa() and atob() functions handle Base64 encoding and decoding for ASCII strings....
🌐
TutorialsPoint
tutorialspoint.com › article › encoding-string-to-reduce-its-size-in-javascript
Encoding string to reduce its size in JavaScript
March 15, 2026 - We need to write a JavaScript function that takes a string and encodes it using a compression format. The function should compare the encoded string with the original and return whichever is smaller.
🌐
Dadroit
dadroit.com › online string to json converter
Online String to JSON Converter
Convert String into JSON format easily and securely. Your data is processed on your device, ensuring privacy.
🌐
CheckSERP
checkserp.com › encode › unicode
Unicode Converter, Unicode Encoding and Decoder | CheckSERP
Online Unicode converter, easy to use unicode encoding and decoder tool. Convert plain text to unicode codes and vice versa.
🌐
Google AI
ai.google.dev › gemini api › openai compatibility
OpenAI compatibility | Gemini API | Google AI for Developers
June 22, 2026 - } ], "tools": [ { "type": "function", "function": { "name": "get_weather", "description": "Get the current weather in a given location", "parameters": { "type": "object", "properties": { "location": { "type": "string", "description": "The city and state, e.g. Chicago, IL" }, "unit": { "type": "string", "enum": ["celsius", "fahrenheit"] } }, "required": ["location"] } } } ], "tool_choice": "auto" }' Gemini models are natively multimodal and provide best in class performance on many common vision tasks. import base64 from openai import OpenAI client = OpenAI( api_key="GEMINI_API_KEY", base_url="
🌐
Telerik
telerik.com › blogs › encode-and-decode-strings-using-javascript
Encode and Decode strings using JavaScript
May 27, 2021 - The escape() method returns a string value (in Unicode format) that represents the encoded contents of the function argument. All spaces, punctuation, accented characters, and any other non-ASCII characters are replaced with %xx encoding, where xx is equivalent to the hexadecimal number representing the character.
🌐
CyberChef
toolbox.itsec.tamu.edu
CyberChef
Maybe you have 10 timestamps that you want to parse or 16 encoded strings that all have the same key.