How to remove single quotes in Struts? - javascript

I am passing a Struts property to my JavaScript function as follows:
facemode('<s:property value="caseemailnumber" />');
I am getting the emailnumber as 'abc#gmail.com'. I want to remove the single quotes. How can I do this?

Use the replaceAll String method and the backslash for the character ', like this:
facemode('<s:property value="caseemailnumber.replaceAll('\'','')" />');
I faced almost the same problem today and it was driving me crazy. Hope it helps.

Use the escapeJavaScript attribute of the tag:
facemode('<s:property value="caseemailnumber" escapeJavaScript="true"/>');
However, as BalusC says, it should be stored correctly in the first place--consider scrubbing your data.

Related

.NET MVC /Javascript escape single quotes from dynamic HTML

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
:)

Pass image url from variable to html in javascript

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'));

How to deal with sigle quote in javascript in jsp page?

onclick= "_deleteWPSchemeData(${viewWPMasterGrid.id}, '${viewWPMasterGrid.name}')"
${viewWPMasterGrid.name} retutrns me a string(for e.g. W.P.WINT OFF ALL'10) which often has single quote character so from the calling javascript method I am not getting the second parameter at all. How to deal with problem?
When a dynamic String can be put inside a JavaScript string literal, it should be JS-escaped. Just as when a dynamic String is put inside a HTML page, it's HTML-escaped.
Use commons-lang StringEscapeUtils.escapeECMAScript (or escapeJavaScript depending on the version) to escape the String. You could create a very simple EL function to do that straight from the JSP.
Note that you could have problems with single quotes, but also double quotes, tags, EOLs, backslash, which must all be escaped in a JS String literal.
It looks like you could split the second parameter out into its own variable first. If I have understood your question correctly.
var viewWPMasterGridName = "${viewWPMasterGrid.name}";
onclick = "_deleteWPSchemeData(${viewWPMasterGrid.id},'" + viewWPMasterGridName + "')";
Use '${viewWPMasterGrid.name.replaceAll("'", "\'")}'
try this,
var name = "${viewWPMasterGrid.name}".replace(/'/g,"\\'");

Whats wrong with this JSON syntax?

Am trying this on my chrome debugger console, and am getting a SyntaxError;
JSON.parse("[{"name":"gath","age":10}]");
>SyntaxError
What is the correct way of parsing the JSON string?
Please note this question is a follow up to my earlier one, which am yet to get an answer!
You need to escape your double-quotes.
JSON.parse("[{\"name\":\"gath\",\"age\":10}]");
or, for better readabilty, define the string with single quotes:
JSON.parse('[{"name":"gath","age":10}]');
JSON.parse("[{\"name\":\"gath\",\"age\":10}]");
You cant have double quotes inside double quotes
You need to escape the "
or do JSON.parse('[{"name":"gath","age":10}]');
Enclose it in single quotes and it will parse correctly.
JSON.parse('[{"name":"gath","age":10}]');
Object
age: 10
name: "gath"
__proto__: Object
Replace
JSON.parse("[{"name":"gath","age":10}]");
With
JSON.parse('[{"name":"gath","age":10}]');

REGEX: Appending Single Space between Capitals in Javascript

I got some idea from a C# related thread, but I need it in Javascript. I am try all sorts of things, it doesn't seem to work.
name.replace(/[A-Z]/g, / $&/);
What I am trying to do is make:
FirstName
with spaces:
First Name
But what I get is:
/ F/irst/ N/ame
Any ideas would be much appreciated.
"FirstName".replace(/([a-z])([A-Z])/g, '$1 $2')
That results in
First Name
Without a leading space.
s.replace(/[A-Z]/g, ' $&')
The second parameter need not be a regex, but a string instead.
Remove the / from the replace section. Some languages need them, some don't.

Categories

Resources