8 replies [Last post]
seb
Offline
Enthusiast
Last seen: 15 years 44 weeks ago
Joined: 2003-09-20
Posts: 208
Points: 0

What's the most correct way of denoting a javascript block in an XHTML page. Obviously want it to pass source validation, but also work in older browsers.

Is

	<script type="text/Javascript"><!--
		function initPage() {return true;}
	//--></script>

sufficient? Or am I missing a trick?

Tags:
Tony
Tony's picture
Offline
Moderator
Brisbane
Last seen: 2 weeks 1 day ago
Brisbane
Timezone: GMT+10
Joined: 2003-03-12
Posts: 5344
Points: 2965

Best way of delimiting a javascript block?

Hi seb,
That looks fine to me.

Where possible keep the scripts in a separate file.

Hugo
Hugo's picture
Offline
Moderator
London
Last seen: 7 years 47 weeks ago
London
Joined: 2004-06-06
Posts: 15668
Points: 2806

Best way of delimiting a javascript block?

Are we not meant to enclose it in with a [cdata] thingy! in order to validate in xhtml,

 <script type="text/Javascript">
//<![CDATA[
      function initPage() {return true;}
   //]]></script>

In the body, or am I confused :?

Hugo.

Before you make your first post it is vital that you READ THE POSTING GUIDELINES!
----------------------------------------------------------------
Please post ALL your code - both CSS & HTML - in [code] tags
Please validate and ensure you have included a full Doctype before posting.
Why validate? Read Me

seb
Offline
Enthusiast
Last seen: 15 years 44 weeks ago
Joined: 2003-09-20
Posts: 208
Points: 0

Best way of delimiting a javascript block?

yeah i've seen the cdata thing before, but wont that break older browsers?

Also is there no need to include "language="Javascript"" for older compatibility?

That said, quite frankly if anyone looks at any of my sites in any 4.x browsers they're having a laugh Wink

seb
Offline
Enthusiast
Last seen: 15 years 44 weeks ago
Joined: 2003-09-20
Posts: 208
Points: 0

Best way of delimiting a javascript block?

Also how important is it to keep your scripts in the <head>

And why in an external file?

brockly
Offline
Regular
Kent England
Last seen: 18 years 15 weeks ago
Kent England
Joined: 2004-08-18
Posts: 22
Points: 0

Best way of delimiting a javascript block?

Im not to sure why, im new to this but i like to make external java scripts to keep my html pages free from clutter plus it helps if more then one page is using the same script.

Tony
Tony's picture
Offline
Moderator
Brisbane
Last seen: 2 weeks 1 day ago
Brisbane
Timezone: GMT+10
Joined: 2003-03-12
Posts: 5344
Points: 2965

Best way of delimiting a javascript block?

Having the JavaScript in an external file has a few benefits such as:
* Easier to validate the page Smile
* Cleaner markup
* Easier to maintain as a separate file
* Reuse scripts across the whole site

I don't have a good understanding of the CDATA tags but it may be required for XHTML Strict.

Hugo
Hugo's picture
Offline
Moderator
London
Last seen: 7 years 47 weeks ago
London
Joined: 2004-06-06
Posts: 15668
Points: 2806

Best way of delimiting a javascript block?

As far as I can gather comment tags are meant to be written using the [cdata] tag as well as script and style tags, something to do with xml seeing the < as a parsed element. I think that this a pedantic interpretation as comments happily validate in xhtml strict but the fact remains that we are not meant to pepper the xhtml body with JavaScript rather remove it to an external file where possible ,so it's probably good practise to use it on body level script.

Hugo.

Before you make your first post it is vital that you READ THE POSTING GUIDELINES!
----------------------------------------------------------------
Please post ALL your code - both CSS & HTML - in [code] tags
Please validate and ensure you have included a full Doctype before posting.
Why validate? Read Me

seb
Offline
Enthusiast
Last seen: 15 years 44 weeks ago
Joined: 2003-09-20
Posts: 208
Points: 0

Best way of delimiting a javascript block?

Tony wrote:
I don't have a good understanding of the CDATA tags but it may be required for XHTML Strict.

I've seen CDATA mentioned in XHTML specs, but I code everything as XHTML1.1 and have had no problems validating without cdata.