Page 89 - HTML5
P. 89

Chapter 21: Include JavaScript Code in HTML




        Syntax



            •  <script type="text/javascript"> //some code </script>
            •  <script type="text/javascript" src="URL"></script>
            •  <script type="text/javascript" src="URL" async>//async code</script>


        Parameters



          Attribute     Details


          src           Specifies the path to a JavaScript file. Either a relative or absolute URL.


                        Specifies the MIME type. This attribute is required in HTML4, but optional in
          type
                        HTML5.

                        Specifies that the script shall be executed asynchronously (only for external
          async
                        scripts). This attribute does not require any value (except of XHTML).


                        Specifies that the script shall be executed when the page has finished parsing
          defer         (only for external scripts). This attribute does not require any value (except of
                        XHTML).

          charset       Specifies the character encoding used in an external script file, e.g. UTF-8


          crossorigin   How the element handles crossorigin requests


          nonce         Cryptographic nonce used in Content Security Policy checks CSP3


        Remarks



        If the embed JavaScript code (file) is used to manipulate
        http://stackoverflow.com/documentation/javascript/503/document-object-model-dom Elements,
        place your <script></script> tags right before the closing </body> tag or use JavaScript methods
        or libraries (such as jQuery to handle a variety of browsers) that makes sure the DOM is read and
        ready to be manipulated.


        Examples



        Linking to an external JavaScript file


         <script src="example.js"></script>






        https://riptutorial.com/                                                                               73
   84   85   86   87   88   89   90   91   92   93   94