JavaScript Ternary Operator


When you have a simple if...else statement like the following:

    var a = true;
    if (a == true) {
    else {

It may be best to use the ternary operator.

Parse class values: Retrieving custom meta data from an HTML element's class name

The problem

I had HTML that had class information on each object that signified how deeply it was indented (e.g. "indent-5"). I needed to use the value of the indent to make various comparisons in another. To solve this problem I created a simple utility function.

NOTE: All functions were written with a dependancy on jQuery.

Prevent the loss of unsaved data with OnBeforeUnload


Sometime last year I learned about the onUnload event, which is available on the "window" object. Today I thought I could use it to prevent a user from leaving a web page if, for example, there is unsaved data that will be lost. I quickly realized that it would not work. The "onunload" event happens after the user has already "unloaded" the page. I needed an event that fired before the user action and naturally came upon, "onBeforeUnload."

Subscribe to RSS - JavaScript