Barada
Barada

Reputation: 252

How to set charset to .js file in MVC ScriptBundle?

I have script.js file that contains several string in cyrillic. When i attempt to load this with standart link like this:

 <script type="text/javascript" src="~/Content/Script/Script.js"></script>

cyrillic letters become rectangulars/badCharsetCaracters.

This solves the broblem:

<script type="text/javascript" src="~/Content/Script/Script.js" charset="windows-1251"></script>

How can I set charset with ASP.NET MVC 4 Bundles? Code like this:

 bundles.Add(new ScriptBundle("~/bundles/scripts").Include(
                     "~/Content/Script/Script.js"));

Upvotes: 6

Views: 4849

Answers (2)

cryss
cryss

Reputation: 4499

Change your script file's encoding to your website's encoding.

For example, if your site is encoded in UTF-8, then:

  1. Open your script file in the windows' notepad
  2. Click File > Save as
  3. Change encoding to UTF-8 (list of encodings is in front od the "Save" button)

Upvotes: 3

nemesv
nemesv

Reputation: 139748

You can use the Scripts.RenderFormat to specify your custom script tag rendering format:

@Scripts.RenderFormat(
    "<script type=\"text/javascript\" src=\"{0}\" charset=\"windows-1251\"></script>", 
    "~/bundles/scripts")

Upvotes: 12

Related Questions