Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Escaping JavaScript string literals in views

Tags:

asp.net-mvc

Is there a utility function for escaping JavaScript in ASP.NET MVC views? I often need to init a little snippet of JavaScript using some values from the view; for instance I may have something like:

<script type="text/javascript"> var page = new Page({ currentUser: "<%= Model.UserName %>" }); page.init(); </script> 

I would expect something like:

<script type="text/javascript"> var page = new Page({ currentUser: "<%= Html.JavaScriptEscape(Model.UserName) %>" }); page.init(); </script> 

I could, of course, write the function myself. But since there are already built-in utilities form HTML encoding, and since one of the selling points of ASP.NET MVC is that the <% %> is the default rendering mode, and since what I'm trying to achieve is quite common, it makes me wonder why I cannot find anything like that already built-in. Is there, for instance, an easy and elegant way to serialize an object to JSON in views?

Or am doing something against ASP.NET MVC principles? When I hit a problem like this, it usually makes it think that either I’m doing something wrong since I assume that the framework designers spent some time thinking about real world scenarios.

like image 623
Jan Zich Avatar asked Sep 24 '09 10:09

Jan Zich


People also ask

How do you escape a string literal in JavaScript?

To escape a backtick in a template literal, put a backslash ( \ ) before the backtick.

How do you escape a string literal?

String literal syntaxUse the escape sequence \n to represent a new-line character as part of the string. Use the escape sequence \\ to represent a backslash character as part of the string. You can represent a single quotation mark symbol either by itself or with the escape sequence \' .

Do I need to escape in JavaScript string?

TL;DR there's no need for crazy hacks like string concatenation or char literal escapes — just escape it as such: var snaphtml = '<\/script>'; Also, note that this is only necessary for inline scripts.

Which is the correct way to create a string in JavaScript?

To declare variables in JavaScript, you need to use the var, let, or const keyword. Whether it is a string or a number, use the var, let, or const keyword for its declaration. But for declaring a string variable we had to put the string inside double quotes or single quotes.


2 Answers

In .NET 4, The HttpUtility class has a variety of static encoding methods for various contexts, including a JavaScriptStringEncode method for this particular purpose.

It's often simpler to just use JSON deserialization, though.

like image 112
StriplingWarrior Avatar answered Sep 28 '22 19:09

StriplingWarrior


In MVC 5 using Razor templates, the following is possible:

<script type="text/javascript">     var page = new Page({ currentUser: @Html.Raw(Json.Encode(Model.UserName)) });     page.init(); </script> 
like image 24
sshine Avatar answered Sep 28 '22 19:09

sshine