This wiki is for the exclusive use of my friends and colleagues. Account creation and anonymous editing have been turned off.

If you are a friend or colleague of mine, and would like to participate in this project, please send me a message.

If you find these pages useful, please donate to help cover server costs. Thanks!

Script Tag

From OdleWiki
Jump to: navigation, search

The <script> tag has three applications:

  1. When used in the <head> element, it can contain JavaScript statements.
  2. When used in the <body> element, it can contain JavaScript statements. This use is not recommended.
  3. When used in the <head> element, it can be used to load external JavaScript files.

In the <head> Element

<!doctype html>
<html>
    <head>
        <script>
            window.alert("JavaScript is active");
        </script>
    </head>
    <body>
    </body>
</html>

In the <body> Element

Again, this usage is not recommended. Best practices dictate that content and function remain separate.

<!doctype html>
<html>
    <head>
    </head>
    <body>
         <button onclick="alert('JavaScript is active');">Click me</button>
    </body>
</html>

In the <head> Element to Load External JavaScript Files

<!doctype html>
<html>
    <head>
        <script src="myscript.js"></script>
    </head>
    <body>
    </body>
</html>

This method does not work within the <body> tag. If you want to load external JavaScript files, you must do so from within the <head> element.

HTML Compliance

HTML5

In Javascript, the proper notation for HTML5 has no attributes:

<script>
   ...JavaScript statements...
</script>

HTML4

When used in HTML4 or XHTML, the <script> tag must include the type attribute:

<script type="text/javascript">
   ...JavaScript statements...
</script>

Deprecated Attributes

At one point, the language="JavaScript" attribute was required. It has long been deprecated and should not be used.