This question already has answers here:
Is there a standard way to encode a .NET string into JavaScript string for use in MS Ajax?
(3 answers)
Closed 7 years ago.
Here how we are getting user company name from session in aspx page
var Details = "AgentCompanyName=" + encodeURIComponent("<%=((Agent_Html5.AgentClassLib.clsAgentSession)Session["UserSession"]).strAgentCompanyName%>");
above code works fine if strAgentCompanyName doesn't contain quote(single,double) but that in it then it not works.
eg. if Agent company name name provided as : David "Mike" Bela solution then it raise error as
it is not appropriate string near Mike keyword.
Details = "AgentLastName= David "Mike" Bela solution"
How to handle for single/double quote?
You need to escape double quotes before output for javascript. So that it will be valid syntax. You should use something like:
<%= yourValue.Replace("\"", "\\\"") %>
Also you can use a better way of escaping with javascript:
var Details = "AgentCompanyName=" + $("<div/>").text("<%=yourValue%>").html();
With this you will create in memory DIV element, set value as text and get as escaped HTML.
Related
This question already has answers here:
Parse JSON in JavaScript? [duplicate]
(16 answers)
Closed 2 years ago.
Please note that this is not a json object :) My string is:
{"message":"***error in SAP module:-1***","status":400}
This is not a json object, this is a pure string. I cannot turn it into a json object due to technical limitations.
So, I want to take only the bold string (all the value of "message").
I thought about lastIndexOf, and pick the string between ":" and ","
But I got messed up with the escape characters for the quotes.
How can I achieve it with lastIndexOf? Or in another better way?
If you can't use JSON.parse, I would use a regex to read it. You know you want the string directly following "message":", so I would look for that, then grab everything from there to the next occurrence of "
This question already has answers here:
Regular expression to get a string between two strings in Javascript
(13 answers)
Closed 2 years ago.
I have a string like this:
let temp = 'Hi {{username}}, Your request with request id: {{requestId}} has been processed. Please contact your nearest shop.'
I want this array from the string:
['userName', 'requestId']
I know I have to somehow use regular expressions to achieve this but I can't figure out the pattern for achieving this.
NOTE: This is just an example string and I want a more general approach to solve this problem coz the string may vary.
You need to use positive lookahead. Here is the regex which works for your case.
let temp = 'Hi {{username}}, Your request with request id: {{requestId}} has been processed. Please contact your nearest shop.'
temp.match(/(?<={{)(\w+)(?=}})/g)
This question already has answers here:
Javascript, Razor and Escape characters. Like apostrophe
(5 answers)
Closed 7 years ago.
I'm assigning the value of JS string variable from server side code.
Like this
var fbShareTitle = "#(ViewBag.LeadTitle as string)";
ViewBag return string value is
A "Fantastic" Lead With 'Qoute'
Now It is giving error in console
SyntaxError: missing ; before statement
I have tried this
var fbShareTitle = ("#(ViewBag.LeadTitle)").replace(/"/g, '\\"');
But now I'm getting this error.
SyntaxError: missing ; before statement
As This string will be shared on fb, So i can't modify string, like replace all " with ' e.t.c.
The reason why your code doesn't work is that Razor will generate the following:
var fbShareTitle = "A "Fantastic" Lead With 'Qoute'";
which is invalid JavaScript. You can't simply fix it by replace, since it's not the problem that your string is bad, it's that your code can't parse - replace never gets to execute. You need to fix it on serverside, where you generate the JavaScript in question, by modifying your Razor code:
var fbShareTitle = #Html.Raw(Json.Encode(ViewBag.LeadTitle as string));
Json will take care of quotes and proper escaping; Raw will make sure you don't get your < and > replaced. Extra benefit from #Html.Raw(Json.Encode(...)) mantra: you can use it to inject any kind of data that can be encoded in JSON, not only strings.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How can I decode a URL with jQuery?
Could somebody please help me to take a string (i.e. URL parameter) and remove the +(plus) or %20 to a regular space (" ")?
For example, to change this:
"my+string+to+change" or "my%20string%20to%20change"
to this:
"my string to change"
I use a lot of jQuery already, so that would be fine. Or pure javascript if that's easier.
Use decodeURIcomponent(stringToDecode)
Note this is not a jquery feature but a regular javascript function.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
ajax post special characters
I am building my own post parameters and passing them via ajax; however, my POST contains an & symbol. How can I post these and escape the special characters so it posts as text only and doesn't split up my value?
Ex:
Thing=lala&lalala
Should be thing = 'lala&lalala' but what I get is thing = 'lala' and ='lalala' where the second key is blank.
You should use encodeURIComponent on your parameters before sending them to the server. It will properly escape everything in your parameters.
Quick example:
var s = 'Thing=' + encodeURIComponent('lala&lalala');
Please note that every value should be encoded separately (so you should not simply use it on the whole query string).