Tuesday, 20 November 2012

JavaScript: check the mime type and charset


I always underestimated JavaScript. I know it has been a problem for me, because I've really missed lots of interesting things until I decided to explore the programming language.
In the following article I will show one of the useful things we can do with JavaScript: discover the charset and mime type of a web page.
You might wonder what's the use of it. Well... as usual try to broaden your view and consider the fact that every visitor is visiting your beloved web site from somewhere in the wide world, sitting on a different kind of chair (if not standing or lying), with a different piece of technology or device, and finally (but I could go on forever) with a specific browser ... ... type and version (the mixture of the two is a deadly issue for any web developer).
Ok, said that, don't you think that knowing the mime type and charset of a page might be useful?
Let's see how to do the magic.

The code
The code is quite simple: we are going to use two properties for the document object: mimeType and defaultCharset.
<script type="text/javascript">
var mT = document.mimeType;
var cS = document.defaultCharset;
with (document) {
write("Mime Type = " + mT);
write("Charset = " + cS);
Just put the above in your document and see what comes out.
Now that you know how to get those values, you might image what you can do. For example we can send the user an alert when the two properties are not met by the target browser.

Ok folks... see ya next time!

0 thoughts:

Post a Comment

Comments are moderated. I apologize if I don't publish comments immediately.

However, I do answer to all the comments.