Monday 27 February 2012

Basic JavaScript Framework


I always wrap my code within a function which creates a local instance of the window. This is then executed when the Javascript file is loaded.

wrapping the window:
( function(window) {
//
}(window));

Create a function to called 'Main' to set-up variables and event listeners prior to the page loading. Here I will add an event listener to check when the webpage has loaded. You need to create a function called 'loadComplete' to execute when the page is loaded. Finally you will need to call the 'Main' function.

( function(window) {
function Main() {
  if(window.addEventListener) {
   window.addEventListener("load", loadComplete);


  } else {
   window.attachEvent("onload", loadComplete);
  }
 }
   function loadComplete(event) {
  //
    }    
Main();
}(window));

This is the basic framework required to start coding an app or a game.

No comments:

Post a Comment