...

View Full Version : are these still use useful?



resin
06-21-2012, 09:05 AM
Hello, I want to be clear on something as I've heard mixed opinions. For modern browsers, is it still necessary to include <!-- //--> or type="text/javascript" for scripts? I still see people using them on this forum and elsewhere sometimes, but then I've also read that they're completely useless now for any modern browsers?

From what I understand the <!-- //--> isn't necessary anymore because any modern browsers will simpy ignore the script if JS is disabled (rather than misreading it as text), and that type="text/javascript" is automatically set as default by all modern browsers and therfore is just redundant to include in the script tag. Is this correct? My pages are script heavy and I've pretty much accepted that they won't be fully functional in very old browsers. But before I do away with these 2 things completely, I wanted to double check if they are still at all beneficial for any modern browsers?

VIPStephan
06-21-2012, 10:16 AM
You can leave out the HTML comments in scripts, that is true. If you write XHTML you should replace those comments with CDATA markers (I’m lacking the right term for that at the moment) since otherwise the parser interprets some characters as HTML:


<script type="text/javascript">
// <![CDATA[
… JS here …
// ]]>
</script>


The type attribute might not be necessary since browsers interpret it as JS by default but it is still required, otherwise the HTML validator will complain. THe only thing that is really unnecessary, and deprecated at that, is language="JavaScript".

felgall
06-21-2012, 08:12 PM
You can leave out the HTML comments in scripts, that is true. If you write XHTML you should replace those comments with CDATA markers (Im lacking the right term for that at the moment) since otherwise the parser interprets some characters as HTML:


<script type="text/javascript">
// <![CDATA[
JS here
// ]]>
</script>


The type attribute might not be necessary since browsers interpret it as JS by default but it is still required, otherwise the HTML validator will complain. THe only thing that is really unnecessary, and deprecated at that, is language="JavaScript".



type="text/javascript" was deprecated long ago - the only reason to use it is that IE8 and earlier don't recongise the correct MIME type of "application/javascript"

If you comment out the CDATA tag like that in real XHTML then the tag will be ignored and the script will be treated as PCDATA. The only reason for using a commented CDATA tag like that is in HTML web pages that are pretending to be XHTML where the tag is there simply to fool the validator - since JavaScript is CDATA in HTML anyway.

rnd me
06-21-2012, 09:07 PM
type="text/javascript" was deprecated long ago - the only reason to use it is that IE8 and earlier don't recongise the correct MIME type of "application/javascript"


ie doesn't need it. you see it because the w3 validator complains if it's missing for xhtml, and some people care about validation...

jmrker
06-21-2012, 09:19 PM
So, for clarification purposes, could you say... :confused:

For HTML4 & Javascript, use...


<script type="text/javascript">
// COMMENTS

JS here

/*
extended
comments
*/

</script>


For HTML5 & Javascript, use...


<script type="text/javascript">
<![CDATA[
// COMMENTS

JS here

]]>
</script>

and for XHTML & Javascript, use ...


<script type="text/javascript">
// <![CDATA[
// COMMENTS

JS here

// ]]>
</script>

Any need for a ' />' as the closing tag for xhtml?

What would the recommended <DOCTYPE ...> tags for each of the conditions above
and should they be included in these forum snippets?

Can the extended comment characters /* */ be use with all conditions?

What are the recommended rules to follow for both forum use and validation use?
Can one or two simple rules be applied to work with ALL the different combinations above?

felgall
06-21-2012, 11:48 PM
For HTML4 & Javascript, use...


<script type="text/javascript">
// COMMENTS

… JS here …

/*
extended
comments
*/

</script>


Yes.


For HTML5 & Javascript, use...


<script type="text/javascript">
<![CDATA[
// COMMENTS

… JS here …

]]>
</script>


No - the cdata tag is only necessary in XHTML so for HTML5 the same wrapper is used as for HTML 4.



and for XHTML & Javascript, use ...


<script type="text/javascript">
// <![CDATA[
// COMMENTS

… JS here …

// ]]>
</script>


again NO. For XHTML with embedded JavaScript (whether XHTML 1.0 or XHTML5) you'd use:



<script type="application/javascript">
<![CDATA[
// COMMENTS

… JS here …

]]></script>

For XHTML with external JavaScript you'd use:


<script type="application/javascript" src="extscript.js"/>

XHTML doesn't need to be able to run jScript so you don't need to use the long deprecated text/javascript and can use the correct MIME type for JavaScript instead.

The /* */ comments can be used anywhere inside JavaScript just as the single line // comments can.

Browsers basically ignore the doctype so which doctype you use with whichever of these doesn't matter as long as you have a doctype. It is the MIME type that the file is serve dwith that determines whether the content is HTML (text/html) or XHTML (application/xhtml+xml).

The best alternative with respect to validation is to keep the HTML and JavaScript in separate files so that you can feed to HTML to the HTML validator and the JavaScript to the JavaScript validator and avoid feeding the JavaScript to the HTML validator.

jmrker
06-22-2012, 12:29 AM
Thank you 'felgall'.

resin
06-22-2012, 09:03 AM
Thank you indeed to everyone. Definitely cleared up some things for me.

felgall
06-23-2012, 12:28 AM
To make it clearer as to what each variant of the script tag means, I have created a web page that shows all the different versions i can think of along with a description of what it is/was for - http://www.felgall.com/jstip118.htm



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum