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 OverflowTry 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);
encodeURIComponent works fine for me. we can give the url like this in ajax call.The code shown below:
$.ajax({
cache: false,
type: "POST",
url: "http://atandra.mivamerchantdev.com//mm5/json.mvc?Store_Code=ATA&Function=Module&Module_Code=thub_connector&Module_Function=THUB_Request",
data: "strChannelName=" + $('#txtupdstorename').val() + "&ServiceUrl=" + encodeURIComponent($('#txtupdserviceurl').val()),
dataType: "HTML",
success: function (data) {
},
error: function (xhr, ajaxOptions, thrownError) {
}
});
Jquery
c# - How to encode a URL in jQuery/JavaScript and decode in ASP.NET - Stack Overflow
Encode URL in JavaScript - Stack Overflow
How can I decode a URL with jQuery? - Stack Overflow
Use encodeURIComponent(str) in JavaScript for encoding and use HttpUtility.UrlDecode to decode a URL in ASP.NET.
In JavaScript:
var url = "mynewpage.aspx?id="+encodeURIComponent(idvalue);
$(location).attr('href', url);
And in ASP.NET
_string _id = HttpUtility.UrlDecode(Request.QueryString["id"]);
I've always found this site wonderfully useful for figuring out which encoding I should be using (probably encodeURI or encodeURIComponent). You should be able to find what you want to use there.
I'm not as familiar with the ASP.NET side of things. These guys probably already have a great answer for you.
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);
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.
Try the decodeURIComponent function:
var decodedUri = decodeURIComponent('http%3A%2F%2Fdtzhqpwfdzscm.cloudfront.net%2F4ca06373624db.jpg');
alert(decodedUri);
Use decodeURIComponent(), for example:
decodeURIComponent("http%3A%2F%2Fdtzhqpwfdzscm.cloudfront.net%2F4ca06373624db.jpg")
It's not jQuery specific, this is a base JavaScript function.
encodeURIComponent should work for you:
var text = "hello, how are you? & fine";
var link = "http://example.com/test#zzzzzzzzz";
var url = "http://twitter.com/share?url=" + encodeURIComponent(link) + "&text=" + encodeURIComponent(text);
$('#twitter a').attr('href', url);
jsFiddle example
encodeURIComponent is not "complete", you can use a custom function like this (taken from http://phpjs.org/functions/urlencode/ ) :
function encode(toEncode) {
return encodeURIComponent(toEncode)
.replace(/!/g, '%21')
.replace(/'/g, '%27')
.replace(/\(/g, '%28')
.replace(/\)/g, '%29')
.replace(/\*/g, '%2A');
}
Example : var url = encode(url_plain_text);
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)
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, '+');
}