Monday, May 10, 2010

Where to Put Code

For HTML5 you use JavaScript instead of ActionScript. The good news? If you already know ActionScript, JavaScript is 99% the same. JavaScript code can live in any one of three places:

Option 1. Put code right in the header of your web page. Just insert code like this anywhere between the HEAD tags in your web page.
<script type="text/javascript">
  <!–
    function helloWorld() {
      alert(‘Hello World!’) ;
    }
  // –>
  </script>

Option 2. Put code anywhere inside the content. This sample shows how to invoke a JavaScript command by clicking on a button. "alert()" is much like the trace command in ActionScript but it pops a window with whatever you "traced".
<input type="button" id="hello-world2" value="Hello" onClick="alert(‘Hello World!’);" />

Option 3. Load the code from an external file. To "include" some code from an external file, place this code anywhere between the HEAD tags in your web page:
<script src="js/helloworld.js" type="text/javascript"></script>

The code in the external .js file does not need any class wrapper or anything like that. Just enter code as though it were is script in the frame of a timeline in Flash. JavaScript has almost exactly the same syntax as Flash. So just start coding like you always would. Except the difference here is that the browser has a whole new object model - no MovieClips, alpha, stage, etc. You are coding for a whole new environment or "DOM". More on this later.

No comments:

Post a Comment