Try encodeURIComponent.

Encodes a Uniform Resource Identifier (URI) component 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).

Example:

var encoded = encodeURIComponent(str);
Answer from Anders Fjeldstad on Stack Overflow
๐ŸŒ
SitePoint
sitepoint.com โ€บ blog โ€บ javascript โ€บ jquery encode/decode url string
jQuery Encode/Decode URL String โ€” SitePoint
February 12, 2024 - jQuery code snippet to encode/decode (convert) a url string (http address) so that they can be properly viewed on a web page. (ie is a html space).
Discussions

Jquery
๐ŸŒ forum.jquery.com
c# - How to encode a URL in jQuery/JavaScript and decode in ASP.NET - Stack Overflow
How do you safely encode a URL using JavaScript such that it can be put into a GET string? Here is what I am doing in jQuery: var url = "mynewpage.aspx?id=1234"; $(location).attr('href', url); An... More on stackoverflow.com
๐ŸŒ stackoverflow.com
Encode URL in JavaScript - Stack Overflow
It is easier to use with a JavaScript object and it gives you the proper URL encoding for all parameters. If you are using jQuery, I would go for the $.param method. More on stackoverflow.com
๐ŸŒ stackoverflow.com
How can I decode a URL with jQuery? - Stack Overflow
It's not jQuery specific, this is a base JavaScript function. ... Save this answer. ... Show activity on this post. You can simply call the standard javascript functions for encoding and decoding respectively. ... Save this answer. ... Show activity on this post. If you URL should also contain ... More on stackoverflow.com
๐ŸŒ stackoverflow.com
๐ŸŒ
MDN Web Docs
developer.mozilla.org โ€บ en-US โ€บ docs โ€บ Web โ€บ JavaScript โ€บ Reference โ€บ Global_Objects โ€บ encodeURI
encodeURI() - JavaScript - MDN Web Docs
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).
๐ŸŒ
Vandelay Design
vandelaydesign.com โ€บ home โ€บ url encode/decode webapp with jquery โ€บ tutorials
URL Encode/Decode Webapp with jQuery - Vandelay Design
Learn how to build a custom UTF-8 encode/decode web app running entirely on jQuery. ... Most developers will know of URL encoding as UTF-8 encoding. This converts non-ASCII characters into a syntax of %NN where N stands for a unique numeric format.
๐ŸŒ
Tutorial Republic
tutorialrepublic.com โ€บ faq โ€บ how-to-encode-url-in-javascript.php
How to Encode URL in JavaScript
Topic: JavaScript / jQueryPrev|Next ยท You can use the built-in method encodeURIComponent() to safely encode a URL in JavaScript. The encodeURIComponent() method encode all characters except the following: A-Z a-z 0-9 - _ . ! ~ * ' ( ) Let's ...
๐ŸŒ
Quora
quora.com โ€บ How-do-I-encode-URLs-while-using-jQueryโ€™s-AJAX
How to encode URLs while using jQueryโ€™s AJAX - Quora
Answer (1 of 2): I understand you want to send the encoded URL through jQuery AJAX method. Encoding of URL can be done right through JavaScript 1 line code. Use the [code ]encodeURI()[/code] function. This code i have listed below: [code]var uri = "my test.asp?name=stรฅle&car=saab"; var res = en...
๐ŸŒ
MDN Web Docs
developer.mozilla.org โ€บ en-US โ€บ docs โ€บ Web โ€บ JavaScript โ€บ Reference โ€บ Global_Objects โ€บ encodeURIComponent
encodeURIComponent() - JavaScript - MDN Web Docs
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).
Find elsewhere
๐ŸŒ
Jquery
forum.jquery.com โ€บ topic โ€บ jquery-jquery-url-encode
Jquery
We cannot provide a description for this page right now
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ how-to-encode-and-decode-a-url-in-javascript
How to Encode and Decode a URL in JavaScript? | GeeksforGeeks
September 2, 2024 - This function takes a string as a single parameter and uses it to decode that string encoded by the escape() function. The hexadecimal sequence in the string is replaced by the characters they represent when decoded via unescape() function.
๐ŸŒ
Frontend Masters
frontendmasters.com โ€บ blog โ€บ encoding-and-decoding-urls-in-javascript
Encoding and Decoding URLs in JavaScript โ€“ Master.dev Blog
The encodeURIComponent() function encodes a URI component, such as query parameters, in which some characters have special meaning and must be encoded. It encodes all characters except the standard ASCII alphanumeric characters (A-Z, a-z, and ...
๐ŸŒ
Infinetsoft
infinetsoft.com โ€บ Post โ€บ url-encoder-decoder-url-code-convertor-in-jQuery โ€บ 1397
url encoder decoder (url code convertor) in jQuery
<script type="text/javascript"> var url = $(location).attr('href'); var encodedUrl = encodeURIComponent(url); console.log(encodedUrl); var decodedUrl = decodeURIComponent(url); console.log(decodedUrl); </script>
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ how-to-encode-and-decode-a-url-in-javascript
How to encode and decode a URL in JavaScript?
April 26, 2022 - encodeURIComponent() ? Encodes all special characters including URL structure characters.
๐ŸŒ
Dwaves
dwaves.de โ€บ 2014 โ€บ 02 โ€บ 13 โ€บ javascriptjquery-url-encode-decode-characters
ยป javascript/jquery: url encode decode characters | dwaves.de
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);
Top answer
1 of 2
6

You should use urlencode() in PHP to encode URI components.

The equivalent function in javascript is encodeURIComponent().

PHP will decode parameters encoded like this automatically.

So, in you PHP:

'<a href="..&imgTitle='.urlencode($row['imgTitle'])

And equivalently, in your javascript:

'&someparam=' + encodeURIComponent(someValue)
2 of 2
5

Why would it need to be done first with PHP? This JS urlencode function is able to be urldecoded from PHP. Encode once, decode once:

function urlencode (str) {
    // URL-encodes string  
    // 
    // version: 1107.2516
    // discuss at: http://phpjs.org/functions/urlencode
    // +   original by: Philip Peterson
    // +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +      input by: AJ
    // +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   improved by: Brett Zamir (http://brett-zamir.me)
    // +   bugfixed by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +      input by: travc
    // +      input by: Brett Zamir (http://brett-zamir.me)
    // +   bugfixed by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   improved by: Lars Fischer
    // +      input by: Ratheous
    // +      reimplemented by: Brett Zamir (http://brett-zamir.me)
    // +   bugfixed by: Joris
    // +      reimplemented by: Brett Zamir (http://brett-zamir.me)
    // %          note 1: This reflects PHP 5.3/6.0+ behavior
    // %        note 2: Please be aware that this function expects to encode into UTF-8 encoded strings, as found on
    // %        note 2: pages served as UTF-8
    // *     example 1: urlencode('Kevin van Zonneveld!');
    // *     returns 1: 'Kevin+van+Zonneveld%21'
    // *     example 2: urlencode('http://kevin.vanzonneveld.net/');
    // *     returns 2: 'http%3A%2F%2Fkevin.vanzonneveld.net%2F'
    // *     example 3: urlencode('http://www.google.nl/search?q=php.js&ie=utf-8&oe=utf-8&aq=t&rls=com.ubuntu:en-US:unofficial&client=firefox-a');
    // *     returns 3: 'http%3A%2F%2Fwww.google.nl%2Fsearch%3Fq%3Dphp.js%26ie%3Dutf-8%26oe%3Dutf-8%26aq%3Dt%26rls%3Dcom.ubuntu%3Aen-US%3Aunofficial%26client%3Dfirefox-a'
    str = (str + '').toString();

    // Tilde should be allowed unescaped in future versions of PHP (as reflected below), but if you want to reflect current
    // PHP behavior, you would need to add ".replace(/~/g, '%7E');" to the following.
    return encodeURIComponent(str).replace(/!/g, '%21').replace(/'/g, '%27').replace(/\(/g, '%28').
    replace(/\)/g, '%29').replace(/\*/g, '%2A').replace(/%20/g, '+');
}