Escape Quotes In Flask [duplicate] - javascript

This question already has answers here:
JavaScript raises SyntaxError with data rendered in Jinja template
(3 answers)
Closed 2 years ago.
I have a python backend which is connected to the website using Flask. So In the python backend I am passing a variable in this manner
return flask.render_template('index.html', code = code)
and I want to use the value of the variable in the JavaScript so I'm doing something like this
<script>
var script = {{code}}
console.log(script)
</script>
mind you I'm passing the value as a string. So For Example I'm Passing the value
TOKEN = " somevaluehere "
as a string in the variable code and when I print it out in the console, this is what I get
TOKEN = ' somevaluehere '
This is causing a lot of problems, I tried escaping the quotes with a \ but nothing works.
Maybe Something Stupid. But can someone please point it out

Jinja is escaping special characters that are control sequences for
HTML (or XML, and thus XHTML) like &, >, <, " as well as ' (see Flask Documentation).
One solution is to mark it as safe like:
{{code|safe}}

Related

Getting string from another string in Javascript [duplicate]

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 "

Get string type ( double or single quotes ) [duplicate]

This question already has answers here:
Are there differences between ' and " [duplicate]
(3 answers)
Closed 4 years ago.
I'm trying to detect the data type and if that data type is string then check if
it is double or single quotes.
Let say i have two strings:
var a = "hello";
var b = 'hello';
How can i detect if the string is double or single quotes in javascript???
I have tried to do so:
typeof a
i get string as output....but i don't know if that string is double or single quotes. I have also searched a alot, but can't find how is that done.
I'm trying to detect the data type and if that data type is string then check if it is double or single quotes.
You can't, that information isn't retained in any way once parsing is complete. They're both just strings. They are completely indistinguishable.
The quotes are purely a source code thing. They say "The text here isn't code, it's the content of a string." Once the string is created at runtime, it's completely irrelevant what source code created it — including the type of quotes used, or even if it was the result of evaluating something else entirely (like a template literal or a function call).

Recognize # as a part of the string in Javascript [duplicate]

This question already has answers here:
Mix Razor and Javascript code
(8 answers)
Razor ViewEngine: How do I escape the "#" symbol?
(5 answers)
Closed 6 years ago.
I am writing a snippet to quickly populate register fields for testing purposes
$("#TestRegister").click(function () {
var rand = Math.floor(Math.random() * 1000) + 1
$("#UserName").val("Tester" + rand);
//other fields
$("#Email").val("example" + rand + "#yahoo.com");// <==trouble
}
I am writing this code in a script section of cshtml view file. However, email portion gives me trouble. It keeps thinking that code beyond the # is a code, thus, giving me an error that yahoo is not part of the code. I tried forcing it to recognize # as a string by placing \ after it, but it gives me parcer errors. Whenever I try search for #, search engines keep ignoring #, and give me unrelated pages.
How do I force # to be recognized as a part of string?
EDIT: Suggested thread refers to how to recognize C# within Javascript. I am interested in how to make # as a part of string text in Javascript. As a workaround, I placed "0" in front of #yahoo.com. I want to know if there is more elegant solution.
try dirty way
String.fromCharCode(64);//returns #

How to escape double and single quote from JS string [duplicate]

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.

Using ‘&’ in POST variables [duplicate]

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

Categories