Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 9 of 9
  1. #1
    Regular Coder
    Join Date
    Nov 2011
    Posts
    154
    Thanks
    5
    Thanked 0 Times in 0 Posts

    are these still use useful?

    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?

  • #2
    The fat guy next door VIPStephan's Avatar
    Join Date
    Jan 2006
    Location
    Halle (Saale), Germany
    Posts
    8,642
    Thanks
    6
    Thanked 1,005 Times in 978 Posts
    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:
    Code:
    <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".

  • #3
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,633
    Thanks
    0
    Thanked 649 Times in 639 Posts
    Quote Originally Posted by VIPStephan View Post
    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:
    Code:
    <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.
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • #4
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,301
    Thanks
    10
    Thanked 586 Times in 567 Posts
    Quote Originally Posted by felgall View Post
    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...
    my site (updated 13/9/26)
    BROWSER STATS [% share] (2014/5/28) IE7:0.1, IE8:5.3, IE11:8.4, IE9:3.2, IE10:3.2, FF:18.2, CH:46, SF:7.9, NON-MOUSE:32%

  • #5
    Senior Coder jmrker's Avatar
    Join Date
    Aug 2006
    Location
    FL
    Posts
    3,077
    Thanks
    38
    Thanked 498 Times in 492 Posts

    Question For clarification purposes...

    So, for clarification purposes, could you say...

    For HTML4 & Javascript, use...
    Code:
    <script type="text/javascript">
    // COMMENTS
    
    … JS here …
    
    /*
      extended
      comments
    */
    
    </script>
    For HTML5 & Javascript, use...
    Code:
    <script type="text/javascript">
    <![CDATA[  
    // COMMENTS
    
    … JS here …
    
    ]]>
    </script>
    and for XHTML & Javascript, use ...
    Code:
    <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?

  • #6
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,633
    Thanks
    0
    Thanked 649 Times in 639 Posts
    Quote Originally Posted by jmrker View Post
    For HTML4 & Javascript, use...
    Code:
    <script type="text/javascript">
    // COMMENTS
    
    … JS here …
    
    /*
      extended
      comments
    */
    
    </script>
    Yes.

    Quote Originally Posted by jmrker View Post
    For HTML5 & Javascript, use...
    Code:
    <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.

    Quote Originally Posted by jmrker View Post
    and for XHTML & Javascript, use ...
    Code:
    <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:

    Code:
    <script type="application/javascript">
    <![CDATA[
    // COMMENTS
    
    … JS here …
    
    ]]></script>
    For XHTML with external JavaScript you'd use:

    Code:
    <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.
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • #7
    Senior Coder jmrker's Avatar
    Join Date
    Aug 2006
    Location
    FL
    Posts
    3,077
    Thanks
    38
    Thanked 498 Times in 492 Posts
    Thank you 'felgall'.

  • #8
    Regular Coder
    Join Date
    Nov 2011
    Posts
    154
    Thanks
    5
    Thanked 0 Times in 0 Posts
    Thank you indeed to everyone. Definitely cleared up some things for me.

  • #9
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,633
    Thanks
    0
    Thanked 649 Times in 639 Posts
    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
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • Users who have thanked felgall for this post:

    jmrker (06-23-2012)


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •