JavaScript: how to set a value containing quotes inside a textbox - javascript

I have an MVC-project where I store data in a database and one of my views contains textboxes to edit this data.
Because of the specifics I can't create the textboxes directly via TextBoxFor(), EditorFor() etc. but have to affect the value with JavaScript, so what i do is write the needed value in the javascript code at page loading, and this code is later on triggered to affect the textbox value.
$("#textboxID").val("#HTML.Raw(Model.value)");
This workes fine until one of strings has got quotes in it.
When i input it directly like
$("#textboxID").val("#Model.value");
it will be HTML-encoded with the quotes written as > & quot; (without space of cource)
What i found out is that the only way to output quotes correctly is by escaping them with backslash \ however i can't seem to find a helper to do that.
Is there a solution? Am i doing anything wrong?
For now, i found a workaround inspired by Filipe Borges suggestion
#Html.Raw(Html.Encode(Model.Libelle).Replace(""", "\\\""))
It's very ugly, but at least it solves the problem, I appreciate anyone suggesting a better solution.

For now, i found a workaround inspired by Filipe Borges suggestion
#Html.Raw(Html.Encode(Model.Libelle).Replace(""", "\\\""))
It's very ugly, but at least it solves the problem, I appreciate anyone suggesting a better solution.

Solved using:
#Html.Raw(Html.Encode(Model.value).Replace(""", "\\\""))
First suggested replacing " by \" using #Model.value.replace(""", "\\\""), but it does not work because the value only contains " after the default html encoding is applied by mvc.
Edit: Final solution by Souhaieb Besbes. Edited mine to not keep it wrong.

You can use single quote to show double quotes as following:
$("#textboxID").val('"'+#Model.value+'"');

Related

Special character issue in page

Problem: Hi, this ‡ character is getting rendered correctly on my webapp.
I have tried using html entity as well but still its showing as ‡ Or ‡.
The caracter ‡ or maybe similar to not equal to (≠) , was a scpecial character.
And need another syntax, to be shown like that.
I can recommend you to use this line of code
<span style='font-size:100px;'>≠</span>
For more details, we can read this link of reference .
Hope it was helpful.
Thank you

Bookmarklet Window.Location change not behaving

Sorry folks, I've been poking around StackOverflow for awhile but can't seem to garner enough information to fix my problem here. I'm not very adept with regex, or the use of javascript in bookmarklets. Hoping someone can help me fix this little problem.
I would like this:
http://www.example.com/a/b/c/d/review?set=123456
To become this:
" " " " " /close?set=123456
Obviously placeholder information (not that hard to figure out what it's for haha), but the sections abcd are ALWAYS there and always the same, it's just the very end piece that changes while retaining the variable number at the end.
Here's what I have that doesn't work:
javascript:window.location=window.location.href.replace(/.+set=(\d+).+/i,'http://www.example.com/a/b/c/d/close?set=$1');
Every time I use the bookmarklet, instead of loading the page properly, it just falls flat and does nothing. However if I manually change the correct piece of the URL, it behaves properly... I think I'm doing something wrong and I think it's very simple.
The numbers at the end are variable in both value and number. Every other piece remains static.
Any advice?
Quicker but no regex:
javascript:window.location.href = window.location.href.replace("review?","close?");
You probably want to replace
/.+set=(\d+).+/i
with
/.+set=(\d+).*/i
The + matches one or more of whatever precedes it.

Insert multiple line breaks into a JavaScript string (regex) (CodeMirror)

I have a few strings and I would like to insert some line breaks into them at certain points.
I figured out a few of the logistics but as a whole I can't seem to crack this problem, probably because I have limited experience with regex.
Basically I have a long string of XML tags that is all on one line. I want to add line breaks at certain points to get the data more formatted and looking nice. I am using CodeMirror to display this data on a webpage but for some reason its all on line #1.
So I need to go from something like this:
<Sample><Name></Name><PhoneNumber><AreaCode></AreaCode><Number></Number></PhoneNumber></Sample>
To something like this:
<Sample>
<Name></Name>
<PhoneNumber>
<AreaCode></AreaCode>
<Number></Number>
</PhoneNumber>
</Sample>
CodeMirror will take care of the rest of the formatting all I need to do is insert the line breaks in the right spot using regex or a loop of some sort. The Tags will or can change so I am guessing regex has to be used.
I have had success inserting line breaks with \n and &#xD but can't seem to get regex to detect the proper locations.
Any help would be greatly appreciated. Thanks.
UPDATE
I overlooked this but the brackets are in fact being sent as < and >
So example tag would look like:
<PhoneNumber>
or
</PhoneNumber>
So basically need to insert a \n after every > that is a closing tag or a beginning tag that contains children tags.
There be dragons here.
I'd like to point you to a very similar question answered awhile ago that does a good job of explaining why you should NOT try to parse XML yourself unless you REALLY know what you're doing.
Use an XML deserializer if you want to get nice line breaks and that sort of thing.
Try this regex pattern:
>\s*<(?!/)
Replacement string : >\n<
UPDATE:
>\s*<(?!/)

Search and replace

I'm working on a new project and i want to do something similar (somehow) to intellitext ads (without popup though). In fact, i want to add links on certain words. The problem is that i don't know how to handle this.
I mean, ok, i get document.innerHTML then what? I can find any word, but some may be inside of tags. I don't want to replace words from textarea or text inside of a link. Same for input, select, and so on.
Also, the way i've managed things, i replace even the html tags (if the word is select for example, the script will replace <select> tag. And i really don't want this :P
So, any idea? Where to start ? What to do? Anything?
I wish a solution that works without any library, or, if is mandatory, to work with jquery (this is what i know).
I think that some regexp may help. The problem is... I never understand them :s
Any help will be appreciated. Thanks a lot!
jQuery does this already!
Just use .text() to get all the text underneath. Documentation and examples are here.

Passing in a text to a javascript function that may have a single quote

I have a link that I dynamically create which looks something like the following:
<a onclick="Edit('value from a text column here')" href="javascript:void(null);">Edit</a>
with the Edit function then taking the passed in value and putting it into a Yahoo Rich Text Editor. This works well except for when there is a single quote in the text being passed. The obvious problem being that the link then looks something like:
<a onclick="Edit('I'm a jelly donut')" href="javascript:void(null);">Edit</a>
Any suggestions on what I can do? I'd rather not stray too far from the structure I am currently using because it is something of a standard (and maybe the standard sucks, but that's another question altogether).
Note: I am using ASP as my server side language.
Convert quote charaters to their HTML equivalents, " etc. before you insert them into the HTML. There's a long list of HTML/XML character codes on wikipedia.
There may well be a function to do this for you, depending on how you're dynamically generating the code: PHP has htmlspecialchars, and though I'm not familiar with ASP etc, I'm sure there are similar routines.
You could just replace the ' with \'
Run the text through an escape function to put a \ before the '. If you're generating the link on the server, we would need to know what server-side language you're using to give more specific advice.
If you're generating the tag client-side as a string in javascript (don't do that, use document.createElement), use the String.replace method on the argument string to pick out characters that will break the syntax you're generating and replace them with escaped versions.
I see at least three additional possibilities:
JavaScript Unicode escape
<a onclick="Edit('I\u0027m a jelly donut')" href="javascript:void(null);">Edit</a>
as \u0027 is the javascript espace character for the quote. The others being less practical:
JavaScript function
<script>function getQuote() { return "'" }</script>
<a onclick="Edit('I' + getQuote() + 'm a jelly donut')" href="javascript:void(null);">Edit</a>
and:
JavaScript global variable
<script>var g_strJellyText = "I\u0027m a jelly donut"</script>
<a onclick="Edit(g_strJellyText)" href="javascript:void(null);">Edit</a>
But I believe you already considered theses solution (or their variants, or the HTML entity escape mentionned by Chris Johnson).
Still, it's worth mentioning.
So it seems there was more going on, most of these would probably work under most circumstances, but in the interest of posterity here's what I had to do.
The information being pulled into the Rich Text Editor was in a YUI datatable and I am creating a link using the custom formatter prototype for said control.
The problem with using the "Replace ' with \' " method was that I need to still view the text being displayed in the datatable the same way.
The problem with using the ASP equivalent of htmlspecialchars (Server.HTMLEncode) was that it still had a problem in the javascript that was being generated from the custom formatter function. I don't know why exactly, but I did try it and it didn't work. That answer was closest to the answer that I came up with (and pushed me in the right direction) so I accepted that answer.
What I did instead was used the javascript function 'escape' to pass into my edit function, and then unescape to set the inner HTML for the Rich Text Editor.

Categories

Resources