I have an HTMl string that is stored in a database. When I go to set the value of a javascript variable with this string on the front-end via my templating engine (Leaf), it stores escaped as:
var string = <p>It's a round about way.</p> <p><!-- pagebreak -->But Maybe this is the way?</p>;
I'm trying to set this value as the content value for TinyMCE, but JavaScript produces an Unexpected EOF error when reading this string and points to a & character, which I presume is the first character of the new line. I tried on the back-end to replace occurrences of string \r\n with a so it would play nicer with JavaScript but the changes didn't seem to take. I tried encoding/decoding the string but that didn't help. Perhaps someone can help shed some light on this seemingly trivial task?
Thanks in advance.
Javascript was rendering \r\n characters found in the string instead of escaping them. Parsed it out in the server-side code instead of handling in JS.
Related
I am in the process of creating a forum. I am doing this without tutorials to test my php, mysql, javascript knowledge and it's going well however I've encountered 1 problem:
I am using encodeURIComponent() to sanitise the posted forum text before sending it via ajax to my PHP and finally through to MySQL. I've noticed that this changes any new lines into the literal carriage return symbol (down and left arrow).
When this forum text is then returned from the database through an ajax request I can see this symbol in my console window but the javascript simply ignores it.
How can I make it recognise this symbol?
Image of the console window
Image of the output HTML
HTML is always ignoring carriage returns, because you are meant to use <p></p> and <br/> for that.
The easiest way to fix your issue is to just replace all carriage returns with line breaks again. You can use str_replace with PHP or str.replace with JavaScript.
I don't have to HTML encode the string. I was trying various solutions, but the problem is still, how do you handle the semicolon entered by the user if you need to do a JS str.indexOf(";"); later on.
I’m using System.Net.WebUtility.HtmlEncode(test); encode my string,which adds semicolons (as you would expect for html encoding).
Later down the process this string gets utilized to create JavaScript commands that end with ';'. These commands are separated by doing a str.indexOf(";");
My issue is that the user is allowed to enter semi-colon in the field,which breaks the aforementioned indexof(";"), which I use to dynamically create the JavaScript commands.
How can I support users entering in semicolons into a string if I need to do a JS indexof(";") to separate the JS commands?
I tried in the C# side doing a
string myString = System.Net.WebUtility.HtmlEncode(test);
but that just makes the situation worse by adding even more semicolons as you would expect for HTML enconding.
The solution I came up with was to do a replace on the the C# side. In C# I do a .Replace of all % and (other problematic characters) with their URL encoded string versions before the JavaScript command ending ";" gets inserted(i.e. myString.Replace(";","%3B").Replace("=","%3D");).
Once it hits the JavaScript side I do the complete opposite, thus leaving my JS semicolons intact.
The aforementioned solution allowed me to distinguish between a user inserted semicolon and one entered in programmatically.
I'm trying to convert Chinese characters in a XML file to readable Chinese string using javascript, but I'm not sure how to. I have checked other SO posts, and tried the following
unescape(encodeURIComponent('丘'))
but still can't get it to work, and wondering if someone could help?
<utf8>丘</utf8>
Neither unescape nor encodeURIComponent (which deal with percent-encoding) will help you with a XML character entity. You just want to parse the XML file! Accessing the DOM then will yield the expected string.
I have to encode string in c# and decode it with javascript unescape function.
the javascript unescape is the only option since I am sending the string with get request to some api that using unescape to decoed it.
i tried almost everything
server.urlencode
WebUtility.HtmlEncode
and a lot other encoding! I even tried Uri.EscapeDataString using jscript
Nothing isn't encode like the "escape" function
Any idea How to make it work?
EDIT:
this is my code
string apiGetRequest = String.Format("http://212.00.00.00/Klita?name={0}&city={1}&CREATEBY=test ", Uri.EscapeDataString(name), Uri.EscapeDataString(city));
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(apiGetRequest);
req.GetResponse();
Can you give an example of the string you want do encode and the encoded result?
URLencoding is the correct encoding-type you need. Make sure, you don't double encode your string somewhere in your code.
You might need to use decodeURIComponent instead of unescape, since unescape is not UTF-8 aware, thus might result in in broken string after decoding.
See http://xkr.us/articles/javascript/encode-compare/ for more information.
EDIT:
I don't know much about asp, but it looks like your trying to access the url not with a browser but with your ASP-server-side application. Well, your server does not run any JS code. You will just retrieve the HTML markup and maybe some JS code as a big string. This code would be parsed and executed within a browser but not within ASP.
I'm trying to have Python run some Javascript, and I am running into a few issues. Namely that when I am trying to inject some text into a textarea of a page, it doesn't seem to work if and only if there is a newline (\n) as part of the variable inserted for the Javascript.
Here's my line:
br.runjs("document.getElementById('edit-body').value = '%s'" % (brag))
if the variable "brag" has any sort of newline in it at all, the insertion does not work. Any idea how to go around this?
Encode brag as JSON and then use that.