codeRambler Ramblings on code, the web, and technology

How do I use the <script> tag?
Friday December 23rd 2005, 3:20 pm
Filed under: Javascript, Web development

I use Javascript every day in my web projects – and have often been asked for details on how to use the <script> tag correctly. The W3C have a formal description of the <script> tag which includes a summary of the required (and optional) attributes.

In the past, browsers have used various techniques to handle the <script> contents… and this has had a long lasting effect on the way we use this tag in our current projects. The most abused attribute remains in common use today (even though has been deprecated in HTML 4.01 and XHTML 1.0 Strict) – the language attribute. Instead of specifying the language, it is now standard to specify the MIME type through use of the type attribute – and it is important to note that this is the only required attribute for the <script> tag.

Inline Javascript

If you are including some Javascript in the document itself, then you would use a construct similar to this:

<script type="text/javascript">
alert("Hello world!");
</script>

If you are delivering this content in a document that is using an XHTML doctype, then you should comment the data between the <script> tags (if you are unable to include it as an external Javascript file – see below) so that it is not parsed client-side. In the past you would often see a similar technique in use to “hide the code from older browsers” (note that the following example shows the correct way to use comments in an XHTML document using CDATA):

<script type="text/javascript">
//<![CDATA[
alert("Hello world!");
//]]>
</script>

There is no point using the CDATA comments above in a non-XHTML page at all. Nor is there any value in using the following kind of comments (whether you are working on an HTML or an XHTML document). This commenting was widely used to prevent “old browsers” seeing the Javascript. Nobody uses these “old browsers” for regular web use (and if they did, most web sites that use Javascript would break anyway)… so there is absolutely no point in including the comments:

<script type="text/javascript">
//<!--
alert("Hello world!");
//-->
</script>

Linked Javascript

If you are including an external Javascript file in the document, then you would use a construct similar to this:

<script type="text/javascript" src="helloworld.js"></script>

The only difference between inline and linked Javascript is that the latter uses the src attribute to specify where the linked Javascript file resides.

Setting a default scripting language

When writing well formed documents, you should include information about the default scripting language. This can be done using a meta tag in the document:

<META http-equiv="Content-Script-Type" content="text/javascript">

This is good practice, but is not required – especially if the server is already serving linked Javascript files as “text/javascript” (or more correctly: “text/x-javascript”).

Comments Off


How do I choose a Doctype?
Tuesday December 20th 2005, 7:55 am
Filed under: Web development

As the W3C reminds us, there are many types of HTML in use on the internet. To ensure these documents are displayed correctly, the various programs (browsers, mobile phones, PDAs etc) that view these documents need to know what kind of document structure they contain. These programs look to the Document Type Definition (referred to as a DTD and commonly called a doctype).

The doctype appears as the very first thing on the page that is delivered to the user, allowing the browser agent to make decisions about how to display the page. Whilst a document can still display within a web browser in the absence of a doctype, defining one implies the document is “well formed” and allows a browser to make assumptions that assist it in rendering correctly. It also prevents the browser from making the wrong assumptions and displaying the document in an unexpected “quirks” mode.

So how do you decide what doctype to use? Again, the W3C have provided a list of recommended DTDs for your document but they don’t offer a lot of guidance to the developer in choosing one. In my opinion it comes down to a question of whether you are creating an HTML or an XHTML document.

Choosing between HTML and XHTML

Unless you are going to be serving your documents as “text/xml” and loading them into an XML parser to allow your XML tools to manipulate the page, then there is no reason to develop your documents as XHTML. A majority of web pages on the internet are HTML – and this should be the default choice for developing your documents – especially if your web host is serving your pages as “text/html”.

Frameset, Transitional or Strict?

If you are developing frameset documents, then the choice is already made for you – you will use a Frameset doctype. The rest of the time you should consider using a Strict doctype which is almost the same as the Transitional doctype, but with the presentation specific elements removed (deprecated).

The big difference between Transitional and Strict doctypes is the seperation of the presentation and content layers within the document. The idea is that you use CSS to define the layout of your document, and rely on well formed HTML to define the content of the document. This ensures that the different programs that may be used to view your document are able to read the content consistently, and apply the appropriate presentation layer (via CSS).

When should you use a Transitional doctype?

The Transitional doctype is appropriate if you are working on an existing HTML document that contains presentational elements (like the <center>, and <font> tags). These tags were totally acceptable for the older HTML 3.2 definition (where the presentation and content layers were freely combined) and many sites still contain documents that have not been “upgraded” to use the most current standards.

If you are starting a new site, then you should be using a Strict doctype rather than a Transitional one.

So tell me what to do!

To summarise, I would suggest that you use the following doctype for each of the following “common” development scenarios:

  • Frameset HTML 4.01 – when maintaining or updating a frameset HTML document
  • Transitional HTML 4.01 – when maintaining or updating an old HTML document that contains deprecated tags
  • Strict HTML 4.01 – when building a new site which seperates the presentation and content layers
  • Frameset XHTML 1.0 – when maintaining or updating an HTML frameset XHTML document
  • Transitional XHTML 1.0 – when building or updating a site that needs to support HTML presentation layer tags
  • Strict XHTML – when building a new site that will be manipulated by an XML parser and served as “text/xml”

The decision of whether to use Strict XHTML 1.0 or XHTML 1.1 is left to the reader. If you are developing Strict XHTML documents and serving them to a wide audience, you should have already made this decision based on the audience and the technology being used to serve (and read/parse) the documents.



Microsoft finally retires IE 5 for the Mac
Thursday December 15th 2005, 1:07 pm
Filed under: CSS, Javascript, Web development

On December 14th 2005, Microsoft quietly published an update onto their web site. They announced they are finally ending all support for Macintosh Internet Explorer as of 31st December 2005 and will not be providing any further security or perfomance updates. Furthermore, the browser will not be available for download from January 31st, 2006. They then go on to actively promote users adopt Apple Safari as a “more recent browsing technology”.

This isn’t really completely new – it has been in Microsoft’s Lifecycle Policy document since at least June 2003 (as seen in this CNet news article from June 2003).

When it was first released in March 2000, version 5.x of IE for the Mac was much more standards compliant than it’s Windows counterpart – and even though Microsoft ended development over 2 and a half years ago, the browser still performed better at rendering CSS in a consistent fashion than more modern Windows Internet Explorer builds. That’s not to say the Macintosh version wasn’t ready for retirement… and without anyone remaining to develop it… it’s loyal users have quietly migrated away.

We now have Apple’s flagship Safari as well as the latest Firefox, Opera and Netscape browsers available for MacOSX as well as a dedicated following of less well-known browsers (including iCab and Camino) that remain in development.

I don’t think the loss of IE is going to be a problem for Mac users and web developers going forward – it will be good to see it finally drop off the web logs, though.

Comments Off


Maori Mix brand cigarettes – from Israel
Thursday December 15th 2005, 8:09 am
Filed under: Ramblings

I like to keep up to date with media around the world. I grew up in New Zealand and miss the local newspapers that I read in my earlier years. They had a very closed view of the world (which is one of the reasons I felt so desperate to leave).

Most of the newspapers in New Zealand are owned by Fairfax New Zealand Ltd. These papers are represented online at http://www.stuff.co.nz and whilst they don’t have every story available online, a good smattering of local content is always on offer.

New Zealand is pretty much an anti-smoking country. Sure, lots of people smoke… but it’s generally not acceptable to just “light up” anywhere you please (unlike my experiences in Europe – and specifically France). So it was rather entertaining to read that Phillip Morris (a global tobacco company) have a brand of cigarettes in Israel called Maori Mix.

The Corporate Social Responsibility in Asia website has an emotive comment from the Maori Smokefree Coalition.

Maori Smokefree Coalition (Te Reo Marama) spokesman Shane Bradbrook said the use of the Maori name and the image was a defilement and unbelievable considering the high smoking rate among Maori, and asked: “Would we have them here and call them Jewish Mix?”

A very good point. These cigarette packets have Maori motifs on them, Pacific Island stylised drawings, a holiday theme and the english text clearly states “Maori Mix”. Ripping off the brand of any identity is insulting… let alone that of the indiginous people of such a “clean green” appearing country as New Zealand.

The story (whilst no doubt a big deal in New Zealand) is hardly that important “in the big picture” of life… but I was glad to then read this transcript from the ABC Radio Australia network where Philip Morris representative, Colin Lipiatt said:

This packaging displayed on a limited number of packs of the Philip Morris international L and M cigarette brand was a short-run edition.

It’s no longer actively on the market in Israel or any other country, nor will it be used in the future.

We regret any offence this packaging has caused and the intent of the packaging was to communicate open-minded acceptance of cultural diversity as a key element of the L and M brand.

I wonder if they would have kept producing them if nobody from New Zealand had complained.

Comments Off