I get some dynamic HTML from a server, that i want to put in an iframe. This works:
document.getElementById('iframe').contentWindow.document.write('#Html.Raw(Data)');
The problem is that the Data (the HTML that i recieve) may contain " and ' which will conflict with my 's surrounding the html-data. Any way to solve this?
A reliable way to encode values in JavaScript in a Razor view is to use Json.Encode():
document.getElementById('iframe')
.contentWindow.document.write(#Html.Raw(Json.Encode(Data)));
Note that there are no ' around the value because Json.Encode() creates a valid JavaScript literal.
I solved this myself.
I used #Html.JavaScriptStringEncode
:)
Related
In my script, text(message.image) returns a dynamic URL path of an image. What would be the correct statement in a javascript?
This fails:
$('<div/>').'<img src=\"'.text(message.image).'\">'.appendTo($('#msgDiv'));
The real answer is:
$('<div><img src="'+message.image+'"/></div>').appendTo($('#msgDiv'));
You have a couple syntactic errata in your code snippet:
You can't access a property with a string like you do.
Concatenation of strings is not with a dot but with a plus.
You are trying to execute text() on a string, not on the div.
I think you're missing (),+,append method and escaping " incorrectly, try with this:
$('<div/>').append('<img src="' + text(message.image) + '"/>').appendTo($('#msgDiv'));
Hope this helps,
I think you have a problem because you're using (double)quote badly
You escape the double quote but you're using simplequote. Try that :
$('<div/>').'<img src=\''.text(message.image).'\'>'.appendTo($('#msgDiv'));
And are you sure concerning the syntax $('').'SOME HTML CODE' ?
You are missing ( and ) there.
Also, you dont need to escape double quotes since you are using single quotes outside.
Try this:
$('<div/>').('<img src="'+text(message.image)+'">').appendTo($('#msgDiv'));
I have html file and it has some labels.when i usually get the values through request parameters appended to URL. when i display the values in an html they will be dispalyed with ASCII chars as below. am getting values with %20 from my URL itself. And am reading them in servlet and forward to some.jsp
Name=Hellow%20how%20are%20you
But i want out put as:
Name=Hello how are you
Do i need to do anything in my html to render the value without ASCII ?
Thanks!
Use unescape():
Name = unescape("Hellow%20how%20are%20you"); // = "Hellow how are you"
Example fiddle
I see that you have servlet here , so you talk about java not javascript i guess.
Any way In java you got StringEscapeUtils from Commons Lang to escape/unescape.
In Javascript you escape through encodeURIComponent, but I think the Commons component I gave to you will satisfy your needs.
I solved it by putting :
URLDecoder.decode(request.getQueryString(), "UTF-8");
I am trying to pass a python dictionary from a chameleon template to a javascript function. But since the dictionary contains single quotes or ' which need to be escaped I get an error in firebug that says : SyntaxError: missing ) after argument list.
My code looks like this:
<div id = "divsfp">
<input type="button" id="sfp" value="SFP"
onclick="get_sfp('${dict_value}')"></input></div>
Where dict_value is a python dictionary. How can I escpae ' in chameleon template before passing the data or in Javascript function itself?
You need to JSON encode the dictionary. You don't then need to put quotes around the dictionary, and JavaScript will see it as a JavaScript object.
Use double-quotes, encoded as ":
onclick="get_sfp("${dict_value}")"
Chameleon will escape any double-quotes in dict_value.
You can try this, if this helps
"get_sfp('"+${dict_value}+"')"
Also from your implementation it seems that the dict_value is the variable you already know. So whats the problem accessing it from the get_sfp function.
Sorry couldn't comment as I still don't have that privilege.
Having a string with several tags given, i have to insert them - if possible at once, without parsing the string to extract the separate scripts:
decodeURIComponent("%3Cscript%20src%3D%27%2F%2Ftest%2Fdg-48119-137488.js%27%20async%20defer%3E%3C%2Fscript%3E%3Cscript%20src%3D%27%2F%2Ftest2%2Feg-48119-137488.js%27%20async%20defer%3E%3C%2Fscript%3E").replace(/+/g,"%20");
I tried with createElement("DIV"), adding the string's content with .innerHTML and appendChild(theDIV). The html content looks fine, but the scripts did not load :-(
What else can I do to get the script post-loaded (without parsing the string before)?
I think this isn't working because the code you posted uses an invalid regular expression.
+ is a reserved character in regex. Try instead using .replace(/\+/g,"%20"); (note the escaped +).
I have a JavaScript hyperlink that is not passing variable to function, undoubtedly due to syntax. Can someone please spot error.
jsfiddle: http://jsfiddle.net/kSVVX/
js
function follow(id){
alert(id);
}
html
<a href='javascript:void(0);' onclick= 'follow('1');'><img src='images/test.gif' border=0 alt='follow'></a>
Note: The reason that I am using all apostrophes is that this link is actually getting echoed from php where a long string is enclosed in quote marks (as certain things in the string must be in apostrophes.) I have a feeling this is source of problem, but have not succeeded in solving it by changing punctuation around.
Thanks for any suggestions.
You are using ' characters to delimit your JavaScript string and the HTML attribute value it is embedded in.
This results in:
onclick= 'follow('
Either:
Avoid intrinsic event attributes (which you should be doing anyway, unobtrusive JavaScript is recommended).
Use different characters to delimit the attribute value (onclick="follow('1');") or string (onclick= 'follow("1");')
Use HTML entities to include the quote mark you are using in the data for the attribute value (onclick= 'follow('1');')