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 #
Related
This question already has answers here:
Parse an HTML string with JS
(15 answers)
Cross-Browser Javascript XML Parsing
(3 answers)
Parse XML using JavaScript [duplicate]
(2 answers)
Trying to use the DOMParser with node js
(8 answers)
Closed 2 years ago.
Is there a feature in javascript such as deleting words starting with "<" and ending with ">"?
For example I want to extract html codes from this object
"description": "<mainText><stats><attention> 25</attention> Hareket Hızı</stats></mainText><br>",
Sounds like a job for regex.
I think this could do it:
// BAD CODE
function useRegex(input) {
let regex = /^<[a-zA-Z]+> <[a-zA-Z]+><[a-zA-Z]+>$/i;
return regex.test(input);
}
EDIT: seems like my idiot self has messed this one up. Don't use regex, as commented by someone else.
I personally hate regex, so here are some tools to help you
Website to help you get started
https://regexr.com/
And how to apply that in Javascript
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions
Regex code creator. It's like magic
https://regex-generator.olafneumann.org/
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}}
This question already has answers here:
Encode URL in JavaScript
(22 answers)
Closed 6 years ago.
I am communicating to a REST server which is expecting something like http://example.com/1.0/editor/5/bla/26 along with body parameters of x, y, and z. Or maybe instead of /1.0/editor/5/bla/26, it should be 1.0/editor/5/bla/what about item #26, but of course with what about item #26 escaped.
There are many post describing how to use jQuery.param() to encode parameters, however, that is not my question. How chould the actual url be created using jQuery of JavaScript?
$.ajax({
type:'PUT',
url:'/1.0/editor/'+$('#id').val()+'/bla/'+$('#wha').data('id'),
data:{x:x,y:y,z:z},
success: function (error){}
});
Can you write a javascript RegEx function to replace all spaces with a known character? Or replace it with its HTML equivalent. Then in your backend, just look for that escape character and replace them with the spaces again. It's kind of a work around but depending on your situation it could work.
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.
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.