Geeks With Blogs
Phubar Baz Musings of an HTML5 Programmer

Now that I've got a few javascript applications under my belt, I'm strating to see a pattern emerge. A javascript app for me always starts with three files. An html, a css, and of course a js file. This is the triangle that makes the base for your application to build upon. The fourth piece that I always include is a reference to jQuery. I wouldn't even try to write a javascript app without it. It's the defacto framework that makes javascript programming tolerable and should be included in any project IMHO.

The first thing I put in my js file is an object to represent my application. Since there's just one instance of the app I just define an object literal, there's no need for a constructor. But I do give it an init() method as the place to get things started.

MyApp={
  init: function() { // do something... }
};

Then of course I add the jQuery document ready handler that calls the object's init() method. The document ready handler is the entry point into the app and is equivalent to the main() function in a lot of other programming languages.


$(document).ready(function() {
  MyApp.init();
});

A javascript application's domain is the html DOM. Use jQuery to manipulate the DOM to make your application do what you want it to do. You can read user input, hide and show elements, and all of the other things that applications usually do. Heck, you can even make games if you want to. I've done it. And with the new additions provided in HTML5 you are going to get even more powerful features like canvas, local storage, etc.

Yes, javascript is a real programming language, and you can write real applications with it. Now go out there and write your own and show us what you got.

Posted on Thursday, August 4, 2011 12:42 PM | Back to top


Comments on this post: Javascript Application Basics

# re: Javascript Application Basics
Requesting Gravatar...
"Yes, JavaScript is a real programming language, and you can write real applications with it."
I love that last part. I've just gotten into Javascript programming after years of work with PHP and Python. I always looked down on JS because I never felt it was terribly useful, -that it couldn't do anything that amounted to much.
Now that I am actually using it though, I am actually very impressed with what it can do. In fact, I just saw on Google, someone made a Gameboy emulator entirely out of Javascript. I had no idea Javascript had that kind of capability!
Anyways, cool post. Im gonna go and check out the rest of the site.
Left by Curtis L. on Aug 04, 2011 2:34 PM

# re: Javascript Application Basics
Requesting Gravatar...
If you want to take your javascript apps to the next level, check out KnockoutJS. It really helps cut out a lot of the DOM manipulation. knockoutjs.com Check out the interactive tutorials section. There's some alternatives too like Backbone.js but I haven't tried them.
Left by Ryan on Aug 05, 2011 12:09 AM

Your comment:
 (will show your gravatar)


Copyright © PhubarBaz | Powered by: GeeksWithBlogs.net