Strange characters in javascript causing it to not load

My site works fine on localhost, my javascript loads and works fine. But when I deploy the site, the script does not work. When I right-click on the page and say the source of the view, and then look at the associated script file, it has some strange characters at the beginning of the file Γ― "ΒΏ (function ($) {

On the local host, my script file starts as (function($){

What causes these characters to appear in my javascript file?

+6
javascript encoding
source share
3 answers

You need to re-save the file in "UTF-8 without specification" encoding. You can use Notepad ++ or other editors.

In the visual studio:

By default, Visual Studio uses UTF encoding using the specification; however, you can save it in a different encoding if you wish. When you go to the Save As dialog box, you can open the Save button to see Save With Encoding. This will offer you a different encoding, and I think that one of the Unicode parameters will not contain the specification (somewhere in the list there is UTF-8 without a signature).

Source: http://forums.silverlight.net/forums/t/144306.aspx

+6
source share

I think Bridis is right about the problem, but I propose a different solution.

When you serve a file, it is served with a Content-type as

 Content-Type: text/javascript;charset=US-ASCII 

?

If so, be sure to use it with UTF-8 encoding.

+1
source share

I just got into the same problem and found a fix.

As an answer to Martjin's question, the problem is that these URF-8 specification characters invalidate javascript when the client expects pure ASCII. Thus, it will be said that there is an error in char 1, line 1, or some such, mainly right at the beginning of the file, because this makes the code-code look like a typo in the first bytes of the script.

In my case, I have a site in IIS, which is an ASP.NET application, and an application under it, which is also an ASP.NET application. This caused some difficulty with web.configs inheritance, and the solution was to put a tag that resets inheritance from that point.

Then I found that all my .js on the child site are throwing an error for this stupid UTF-8 encoding character, which was the first 3 bytes of each file. I'm pretty sure that this is caused by some confusion in httphandlers from my 2-tier solution for web.configs.

My solution, however, was to convert the .js files back to pure ASCII, like the one that IIS sent and expected from the client. For me, I have build.bat, which copies all web files, deletes any source controls and project files, and puts them all in the final assembly directory for copying to a test or production server. I made changes to this script to convert all .js files to ASCII format.

It uses a combination of a DOS batch (because I started) and PowerShell (because this is the only way I converted even more utilities without adding):

set DIRTOCONVERT = any way you want to convert all files for ECHO removes UTF-8 specification characters? "? from the front files for / r% DIRTOCONVERT% %% g in (* .js) do (powershell -command" gc -en utf8 \ "%% g \" | Out-File -en ascii. \ tmp.txt " move / y. \ tmp.txt "%% g")

Note that several people on the network (even on StackOverflow) tried: type badfile.txt> goodfile.txt

but still carries UTF-8 encoding. Apparently, he did not.

+1
source share

All Articles