Category Archives: JavaScript

Removing Debug Statements from Javascript via compiler

We all know the pain of getting a bug filed against some of your code and having to dig through it later to find the root cause. Especially, since many times we end up having to add back in all of our debug statements in order to track down the issue. There has to be a better way. What if we could write all of our JavaScript with the debug code left in?

The Better way

By now, we are all using some compiler to compile your code. Right? If you haven’t started, grab the Closure Compiler to follow through on this example.

  1. Download the latest Closure Compiler http://closure-compiler.googlecode.com/files/compiler-latest.zip
  2. Unzip
  3. Copy the compiler.jar file into your directory with the JS Files.
  4. follow along below and enjoy!

If you are using Closure Compiler or Uglify already, you are in luck! Both of these compilers allow you to annotate your JavaScript. Annotating your files allows you to describe them to the compiler. Things like @license will make sure that the comment goes to the top of the file after compression. Look through the http://code.google.com/closure/compiler/docs/js-for-compiler.html list to see what each one does. We are going to focus on @define.

Continue reading

Getting A Class From a DOM Element

Lately, if I am searching for a way to do something in Javascript the only results I am getting ways of doing the procedure using jQuery. While, its true jQuery is pretty much the de facto standard for writing JavaScript anymore, understanding the underlying technology is a must. As well as alot of times, I don’t need to load jQuery on the page just to get a DOM Element or its class.

So I recently went looking on how to get class from element in JavaScript. Turns out most of the results were for getting elements by class name, rather than actually getting the class of the element I already had.

Here is how you get a class name from an Element using JavaScript.

Continue reading

Lazy Loading Functions

The best part about this Javascript pattern is the ability to calculate once and then use the new function definition from that point forward.

During the course of building web apps, we usually end up with functions to do certain tasks. However, since those tasks sometimes need to be done differently in each of the browsers, we end up with large if else statements to get the job done.

This is where Self Defining Functions come in handy.

Continue reading

Patterns for enforcing “new”

Recently I was looking around for a book on JavaScript Patterns. I decided to give the JavaScript Patterns by Stoyan Stefanov a try. If you have been looking for a book on Javascript Patterns, I highly recommend this book.

In the 3rd chapter, Literals and Constructors, I noticed this section on “Patterns for enforcing new” with the following entry:

When your constructor has something like this.member and you invoke the constructor without new, you’re actually creating a new property of the global object called member and accessible through window.member or simply member. This behavior is highly undesirable, because you know you should always strive for keeping the global namespace clean.

Continue reading

Writing and Updating DOM Elements in a Frame

Handling writing and updating objects in an iframe can be bothersome in most browsers. Sure things like jQuery tend to help, but not always.

A normal way to access a frame in jQuery is below:


$("#FRAME").contents()

Now, lets say you actually want to get another element inside of the frame, lets get the HEAD element.


$("#FRAME").contents().find("HEAD");


Continue reading

$(“#ID”) is fast enough

We all know that the fastest selector to use is the ID. Something like $(“#id”) is better than using a class. However,
after looking at the last test we did on The Great Assumption – JavaScript I noticed
a large discrepancy in $(“#ID”) versus the native document.getElementById().

Now, I always assumed it was slower, but not anywhere near close to what I saw. So, lets challenge the assumption that $(“#ID”) is good enough.

Continue reading