Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6890663
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T06:18:40+00:00 2026-05-27T06:18:40+00:00

How do I properly encode JavaScript in the following context: <html> … <script type=text/javascript>

  • 0

How do I properly encode JavaScript in the following context:

<html>
...
<script type="text/javascript">
var settings = @Html.PleaseEncode(settings.ToJson());
// ...
</script>
</html>

The values in my JSON objects are set by the application administrator, so I assume they need properly encoded — both for HTML and JavaScript.

I’m using System.Web.Script.Serialization.JavaScriptSerializer to do the JSON encoding.
It looks like JavaScriptSerializer does some encoding as it outputs the text <None> as \u003cNone\u003c, but I’m not sure how safe it is. Right now, I’m using @Html.Raw as it works given safe input. It generates the following:

var settings = {"UnselectedReason":"None Selected", /*...*/};

If I use @Html.Encode I then get:

var settings = {&amp;quot;UnselectedReason&amp;quot;:&amp;quot;None Selected&amp;quot;, /*...*/};

I’ve tried with and without AntiXSS but I see no difference either way.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T06:18:40+00:00Added an answer on May 27, 2026 at 6:18 am

    AntiXSS has JavaScriptEncode, but it’s designed for individual items, rather than taking a whole set of, err, settings.

    So if you passed in {“UnselectedReason”:”None Selected”, /…/} it’d eat the quotes and other things, which is probably not what you want. Instead what I’d do is in your ToJson I’d build the settings up with a string builder, something like

    StringBuilder sb = new StringBuilder();
    sb.Append("{");
    foreach(KeyValuePair kv in mySettings)
    {
        sb.Append("\"");
        sb.Append(Microsoft.Security.Application.Encoder.JavaScriptEncode(kv.Key, true);
        sb.Append(":");
        sb.Append(Microsoft.Security.Application.Encoder.JavaScriptEncode(kv.Value, true);
        sb.Append("\",");
    }
    
    string outputString = sb.ToString().TrimEnd(",") + "}";
    
    return new HtmlString(outputString);
    

    Note: Code is off the top of my head and hasn’t been even typed into VS. It illustrates the principal and may well not compile!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Ho would I properly encode the following: # # -*- coding: utf-8 -*- >>>
How do you properly ensure that a user isnt tampering with querystring values or
The following code executes properly when the data key has no data to send,
I'm using AS3 to base64 encode a JPG and pass it to Javascript. I'm
I am using XMLFormat() to encode some text for an XML document. However, when
I'm writing code that automatically generates HTML, and I want it to encode things
I have the following script, which encodes some of the value it receives propertly,
I am passing values in a URL query string that are interpreted by JavaScript
I need to encode a Java String to display properly as part of an
I have a Perl script that has (skipping many irrelevant lines) use HTML::Entities; my

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.