Wednesday, April 26, 2017

When JavaScript files mysteriously fail to load only on some HTML pages

I've been trying to get a contact form onto one of my web sites, and since I would prefer that my e-mail not be flooded with spam, I added reCaptcha to the page. It was very easy to set up, and it worked as intended when testing on my local IIS server as an ASPX page.

My web host, however, does not support ASP.NET, so I have a script that converts a master page plus loose ASPX files into HTML files. I found that the reCaptcha control simply did not appear on the web host, nor did it appear when opening the local HTML file. It didn't even work when I copy-pasted the entire client HTML from the working ASPX page over the contents of the HTML file. It reported a SyntaxError on the first line of Google's script. This happened the same in all browsers I checked.

After being 100% baffled for hours, I finally noticed a tiny, tiny difference between working and non-working documents: the "encoding" status bar field in Notepad++. The working ASPX document was UTF-8; the non-working HTML files were UTF-16LE. Copying the working markup from IIS didn't have an effect because I pasted it into an already-UTF-16LE document instead of making a new file and saving over the old. Once I re-encoded the HTML file and saved, reCaptcha showed up.

I adjusted the script to produce UTF-8 files with the -Encoding parameter on Out-File.

No comments:

Post a Comment