Charset issue: external javascript file drops non-ASCII characters in HTML - javascript

Javascript strings with non-ASCII characters work as expected successfully when placed inside a tag in the HTML file, e.g.
<script type="text/javascript">
alert('tänään');
</script>
That is, an alert window pops up with the word 'tänään'.
However, if the code linked externally in a javascript file, e.g.
<script src='js/alert.js' type="text/javascript"></script>
the 'ä' characters are replaced with null-glyph characters ('t�n��n'). Using the charset attribute does not fix it:
<script charset="UTF-8" src="js/alert.js"></script>
Why is this happening? What fixes are available?

While adding a charset indicator for the browser, like:
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
All text based files (html, js, and oh what the heck css) should be converted to utf-8 as well.
Keep in mind to convert the files to UTF-8 without BOM to avoid those annoying empty lines that will appear on some browsers.

Related

Textedit HTML source code not triggering javascript

Just getting back into coding after a very long break (approx 10 years) so apologies for any incorrect terminology / if this is a basic question.
-Max OS X
-Text Edit
-Google Chrome
Working on a course on Lynda on programming fundamentals. When I type the HTML source code like for like, my version doesn't trigger the .js file, whereas if I c+p the source code from the tutorial file, it does.
Using Textedit as my editor. Very confusing as it is very simple code, and I have copied it identically, but it doesn't react the same way when I type it myself. The rest of the HMTL loads fine but the .js script just doesn't load.
<html>
<head>
<title>Simple Page</title>
</head>
<body>
<p>This is a very simple HTML page</p>
<script src="script.js"></script>
</body>
</html>
I've uploaded the files to mediafire. Container.html is from the tutorial. Container1.html is the HTML file I have typed out myself. Have been scratching my head over this for a while and can't figure out what the difference is between the 2 files except the size (4 bytes).
http://www.mediafire.com/file/p4sz93u9g6admwn/Textedit_JS_Problem.zip
Thanks!
H
It's because of double quotes in your Container2.html. So its seems the code changes its format from UTF to a different one. So what you need to is, open your container2.html file, and go to this line
<script src=“script.js”></script>
Change it to below by deleting double quotes and typing again:
<script src="script.js"></script>
Notice the change in double quote ;)

Charset UTF-8 for external JavaScript file

I add an external JavaScript file like this:
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<script charset="utf-8" type="text/javascript" src="js/panel.js" ></script>
</head>
But the text stored in panel.js file is displayed like this in the HTML file:
H69�*
When I put JavaScript code in the HTML page, the text is displayed properly. But when I put JavaScript code in panel.js, it does not work.
Then the file maybe is not properly saved as UTF-8.
$ file -i panel.js
What's the output?
If you use, for example Vi, open the file and save it with:
:w ++enc=utf-8 %
in the mac OS it is a capital -i...like this:
file -I

Force String in JavaScript to be UTF-8?

my JS-File is UTF-8 encoded, my html document also.
I also have this in the head part of the HTML:
<!doctype html>
<html lang="de">
<head>
<meta charset="utf-8" />
Every umlaut that I typed in the HTML is displayed properly.
But if I try to set a new value with $('#test').text('Test öäü') I get Test öäü.
How is this possible? Even console.debug/log and alert() display the same.
I also tried to set the charset attribute on my script-tag, but nothing worked.
Can anybody help me with this issue?
I found the issue. I use minify to combine my css/js and minify had $min_serveOptions['contentTypeCharset'] set to iso-8859-1.

Print japanese with jQuery

I'm trying to print some Japanese to a page dynamically using jQuery, and it display nothing recognizable. I don't know what went wrong, I reduced the code to the most straight-forward, and it doesn't fix it. Or maybe it's just me being thick.
I use:
$('body').append('<p>日本語</p>');
Which should work, right?
And I get:
日本語
Huh?
Have you made sure that your page is set to use the Japanese character set? If not, make sure that your charset is defined in your <head></head> node :
<meta http-equiv="Content-Type" content="text/html; charset=euc-jp">
In some cases Utf-8 character set would be helpful. Add this in your Head node as below.
<head>
<meta charset="utf-8" />
</head>

Issue loading external files (CSS and JavaScript) in Safari

I'm having a bizarre situation here where my external site files, both JavaScript and CSS, are being interpretted as somehow corrupt on Safari browsers. The same site looks fine in Internet Explorer, but on Safari the javascript and css are being completely ignored. If I look at the files through the Inspector, I can see that Safari views these files as containing junk characters -- either asian symbols or square boxes. When I navigate directly to the files, however, they appear just fine. See the site below, viewed in Safari 5, for an example:
http://amsoell.com/dch/castlemoore
If I embed the same exact CSS or JavaScript into the HTML itself, it renders perfectly. I don't know if this is a server issue or something in my CSS, but I'm at my wits end here.
I checked your website for validation and it's not valid. But the most interesting thing is that your enconding is utf-16le. You should put your encoding to utf-8. Just put the following line in your <head> section:
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
Include the charset="utf-8" parameter in your <script> and <link> tags:
<script type="text/javascript" charset="utf-8" src="script.js"></script>
<link rel="stylesheet" type="text/css" charset="utf-8" href="style.css">
The problem can be solved by saving the html page in "Windows-1252 Encoding".

Categories

Resources