Failed to decode string from C # HttpUtility.JavaScriptStringEncode in javascript
I generate html via c #
myStr = "<span class='red'>September 1980</span><br /><div>abcdef\nhijklm</div>";
shtml = "<span class='red' title='<pre>" + HttpUtility.JavaScriptStringEncode(myStr, false) + "</pre>' id='" + jc.FirstOrDefault().UserId + "'>" + content + "</span>" + after;
... snip snip ...
<%= shtml %>
And my jquery script to initialize qtip:
$('[title!=""]').each(function(){
$(this).qtip({
hide: {
fixed: true, delay: 300
}, show: 'mouseover',
position: {
my: 'top center',
at: 'bottom center',
viewport: $(window),
adjust: {
method: 'shift shift'
, screen: true
}
}, style: {
classes: 'qtip-light', // Inherit from preset style
tip: 'topCenter'
}
});
});
Now the tooltip shows:
\ u003cspan class = \ u0027abcd \ u0027 title = \ u0027September 05, 2013 12: 06 \ u003e \ u003ci
How can I display html in a tooltip? it ate my time and brains ... please help!
Note: please read the following before marking this question as a duplicate:
I searched for all related posts, but none of the soutions worked for me. My use case is different since I use qtip to show the string generated by javascriptstringencode.
, HttpUtility.JavaScriptStringEncode. , JS .
String.prototype.replaceAll = function(str1, str2, ignore) {
return this.replace(new RegExp(str1.replace(/([\/\,\!\\\^\$\{\}\[\]\(\)\.\*\+\?\|\<\>\-\&])/g, "\\$&"), (ignore ? "gi" : "g")), (typeof(str2) == "string") ? str2.replace(/\$/g, "$$$$") : str2);
}
function decodeJs(encodedString) {
var decodedString = encodedString;
decodedString = decodedString.replaceAll("\\u0026", "&");
decodedString = decodedString.replaceAll("\\u0027", "\'");
decodedString = decodedString.replaceAll("\\u003c", "<");
decodedString = decodedString.replaceAll("\\u003e", ">");
return decodedString;
}
function replaceText() {
$("*").each(function() {
if (!$(this).children().length) {
$(this).text(decodeJs($(this).text())).val(decodeJs($(this).val()));
}
});
}
$(document).ready(replaceText);
$("html").ajaxStop(replaceText);<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>