Funny characters in my javascript file - javascript

This character () gets added to my javascript file. i am using visual studio 2010.
has any one come accross this issue?
This link explains my problem and a solution
http://forums.silverlight.net/t/144306.aspx
Cheers

The js file is encoded as UTF-8 and has a byte order mark (BOM) identifying it as such.
This shouldn't be a problem so long as you serve the file up as UTF-8.
If the page doesn't already use a meta tag with the content type set to UTF-8, you can add this information to the script element, as described here:
<script src="js.js" type="text/javascript" charset="utf-8"></script>

The characters added are BOM characters,are not advised for utf-8. Visual studio by default saves javascript files with "Unicode(UTF-8 with signature)- Codepage 65001" encoding, this encoding adds the UTF-8 byte order mark at the beginning of the file. ie9 and chrome do not have a problem with it but firefox, opera and safari completely break....
so to fix it follow the solution in this document
http://forums.silverlight.net/t/144306.aspx and select the encoding option "Unicode(UTF-8 without signature)- Codepage 65001".
Hope this Helps.
Cheers

Related

How to correctly include a javascript file into html

I am trying to run a script through HTML but I am having some problems. I searched online and found how to do so, however the issue is that even if I correctly type the path of the .js file, it seems to add some strange characters before it.
This is in index.html
<script type="text/javascript" src="fractalTest/fractalTest.js"></script>
I expected this to work but when I open index.html in google chrome and inspect then look under the elements tab, this "â©fractalTest/fractalTest.js" is replacing "fractalTest/fractalTest.js" in the path of the file. I believe this is whats causing the error but I do not know how to fix it!
...it seems to add some strange characters before it.
That usually means that the file is saved with a byte-order mark (BOM) of some kind, but isn't being sent with the correct charset for that byte-order mark.
Be sure that the server is configured to serve the files with a specific encoding (UTF-8 is a good choice), and that you save the files using that encoding (in your text editor, etc.). It's also usually best not to include a BOM on UTF-8 files (although it's valid, some tools don't handle it well).
Side note: No need for the type attribute. The default is JavaScript.

Google Apps Script: How to fix "content is not allowed in prolog"

I am trying to parse the following XML file with Google Apps Script XmlService:
<?xml version="1.0" encoding="UTF-8"?>
<Report Major="1" Minor="0" Revision="1">
<CoIDs> ….
Code snippet:
function parse(txt) {
var document = XmlService.parse(txt);
var root = document.getRootElement();
//...
}
When running the script, I get the error message: Content is not allowed in prolog.
Is there a issue with the format of the XML file?
How can I parse this file with Google Apps Script?
Update
I managed to solve the issue by opening the file and saving it again as UTF-8 document with Apple TextEdit. Is there any "automatic" (or code-based) way to convert a non-UTF 8 (presumably UTF-16 document) to UTF-8 before reading it with Google Apps Script?
It is possible to choose the char set of a file when you open it in your drive, I found this info in a post answer by a Google engineer (Corey G) so even if I didn't test it I think he's a trustful source :-) .
The post is here and the code goes as follow:
DocsList.getFileById(<some id>).getBlob().getDataAsString("UTF-16");// replace by the Charset you want... UFT-8 for example.
So I guess it's worth trying...
Let us know if it solves your problem.
I ran into this issue too, I fixed it by removing the BOM (Byte order mark) from the document in my editor. This fixed the problem for me. I guess the parser sees the BOM as content or something.
I had the same problem, I've downloaded Notepad++ and configured the encoding to 'Encode in UTF-8'. You can find it by opening your xml file and selecting Encoding tab -> 'Encode in UTF-8'.
It'll be effective for all xml files in the future.
Hope it finds you well.

Icelandic language messed up in javascript alert box

i have an alert box which i want to show some icelandic text but its not showing it
<script>
function check() {
alert("Þú verður að vera skráð/ur inn til þess að senda skilaboð");
}
</script>
it is showing the alert box but the text is messed up :(
Þú verður að vera skráð/ur inn til þess að senda skilaboð
any help please :(
Today the web uses many international languages and has settled on using UTF-8 (a flavour of unicode) for character encoding. This is important.
You are using iso-8859-1, the MS Windows character set. If you have Word 2007 or 2010 you have the option of re-saving your text as UTF-8. If you've ever seen ????? or � instead of text on someone's web site, it's due to the wrong encoding type.
<meta http-equiv="Content-type" content="text/html; charset=UTF-8"/>
Always use UTF-8 end-to end. Do not use 8859-1 or Windows 2151 encoding.
See:
The Absolute Minimum Every Software Developer Absolutely, Positively Must Know About Unicode and Character Sets (No Excuses!)
ISO-8859-1 vs UTF-8?
Character encodings and the beauty of UTF-8
Let's do it in html5 manner :)
<!doctype html>
<html>
<head>
<meta charset = "utf-8" />
</head>
encoding hazards my best guesses:
check if...
the js-file is stored properly encoded (UTF8) on your server
the server delivers the JS file with proper encoding header (HTTP/1.1 about encoding)
... #Diodeus is right
What you see displayed by the alert is UTF-8 encoded text misinterpreted as windows-1252 encoded. (Windows-1252 is a Microsoft extension to ISO-8859-1.)
If your pages are ISO-8859-1 encoded, as they apparently are, then this applies to the script element content too. There is something odd going if the code you posted does not work. Are you sure the element is really inside a normal page of yours where Icelandic characters work OK? You should not try this fix the situation with a shot in the dark like changing encodings without knowing what is going on.
I’m just making a guess: the alert() invocation is really in an external .js file, which is UTF-8 encoded but treated by browsers as windows-1252 encoded. Then there are two alternative fixes: 1) open that file in an editor and save it as windows-1252 or ISO-8859-1 encoded; or 2) modify server settings to declared UTF-8 for .js files or (less reliably) add charset=utf-8 attribute to the script element.
Alternatively, if the alert() invocation is really inside a script element in an HTML file, then perhaps this file is really UTF-8 encoded but you don’t observe other problems because the content of the file does not otherwise contain Icelandic characters. In this case, it is best to open the HTML file in your authoring program and change its encoding to windows-1252 or ISO-8859-1.

Javascript character encoding

In an external javascript file I have a function that is used to append text to table cells (within the HTML doc that the javascript file is added to), text that can sometimes have Finnish characters (such as ä). That text is passed as an argument to my function:
content += addTableField(XML, 'Käyttötarkoitus', 'purpose', 255);
The problem is that diacritics such as "ä" get converted to some other bogus characters, such as "�". I see this when viewing the HTML doc in a browser. This is obviously not desirable, and is quite strange as well since the character encoding for the HTML doc is UTF-8.
How can I solve this problem?
Thanks in advance for helping out!
The file that contains content += addTableField(XML, 'Käyttötarkoitus', 'purpose', 255); is not saved in UTF-8 encoding.
I don't know what editor you are using but you can find it in settings or in the save dialog.
Example:
If you can't get this to work you could always write out the literal code points in javascript:
content += addTableField(XML, 'K\u00E4ytt\u00f6tarkoitus', 'purpose', 255);
credit: triplee
To check out the character encoding announced by a server, you can use Firebug (in the Info menu, there’s a command for viewing HTTP headers). Alternatively, you can use online services like Web-Sniffer.
If the headers for the external .js file specify a charset parameter, you need to use that encoding, unless you can change the relevant server settings (perhaps a .htaccess file).
If they lack a charset parameter, you can specify the encoding in the script element, e.g. <script src="foo.js" charset="utf-8">.
The declared encoding should of course match the actual encoding, which you can normally select when you save a file (using “Save As” command if needed).
The character encoding of the HTML file / doc does not matter any external ressource.
You will need to deliver the script file with UTF8 character encoding. If it was saved as such, your server config is bogus.

JavaScript Character Issue When Filling Dropdown With jQuery From External JS File

I'm running into a character encoding issue when I load a dropdown using jQuery from an external js file. This only seems to happen when the JavaScript object is not within the page.
For example the below is the JavaScript object.
var langs = [
{value:'zh-CN', text:'中文 (简体) Chinese Simplified'},
{value:'en', text:'English'},
{value:'eo', text:'EsperAnt'},
{value:'es', text:'Español'},
{value:'ja', text:'日本語 (Japanese)'},
{value:'pt-PT', text:'Português'},
{value:'ru', text:'Русский (Russian)'},
];
If this is in my page with the proper meta tags <meta http-equiv="content-type" content="text/html; charset=utf-8" /> the below code works.
$(document).ready(function() {
// Fill language select
$.each(langs, function(i, j){
$('#LangSelect').append($("<option></option>").attr("value",j.value).text(j.text));
});
But, since I need languages on more then one page I've moved the langs object to an external js file and reference it. After doing this, I run into encoding issues such as russian characters become РуÑÑкий (Russian).
This encoding issues seems to still appear even when the reference to the external js file is set as below:
<script type="text/javascript" charset="UTF-8" src="externalJS.js"></script>
Is there anyway to force the JavaScript object to be loaded with the proper encoding from an external file?
Please note I am experiencing these issues when viewing content on the iPhone Mobile Safari browser. Additionally these pages are simply html and JavaScript without any server side components.
Thanks in advance,
Ben
Is there anyway to force the JavaScript object to be loaded with the proper encoding from an external file?
Yes, the script charset attribute as you quoted. However it historically didn't work everywhere and was best not relied on. Where this is not supported, the browser will always use the charset of the main page as the charset in the script. So as long as you include the UTF-8 charset parameter in the main page you should be fine either way.
I am surprised if a modern browser like Mobile Safari doesn't understand it, though.
Is it possible your server might be serving .js files with a bad Content-Type header containing a wrong charset? A combination of unset mime-types for JS plus AddDefaultCharset in Apache could leave you with:
Content-Type: text/plain;charset=iso-8859-1
Which might maybe have the effect of mucking it up.
Make sure you save the javascript file using UTF-8 encoding. If you open the file in Notepad++, then you can click Format>Encode in UTF-8 (If you try Format>Convert to UTF-8, then have a look at the page using a hex editor. Sometimes you end up with some strange characters at the beginning of the file).

Categories