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?
Best way of delimiting a javascript block?
Hi seb,
That looks fine to me.
Where possible keep the scripts in a separate file.
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.
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
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?
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.
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
* 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.
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.
Best way of delimiting a javascript block?
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.