Search code examples
jqueryjavascript-framework

What sorts of javascript/jquery methods do you routinely code into your sites?


I'm coding a core javascript object for my site, building in the common methods I use (and wrapping a few jQuery methods as well).

It's built like this:

var Core = {
  baseUrl: '/',
  lang: 'en-us',
  loggedIn: false,

  msg: function(str) {
    for (var i = 1, len = arguments.length; i < len; ++i) {
      str = str.replace("{" + (i - 1) + "}");
    }
    return str;
  },
  include: function(url, success, cache) {
    $.ajax({
      url: url,
      dataType: 'script',
      success: success,
      cache: cache !== false
    });
  },
  etc...
}

msg is a method to mimic C# String.Format, include lets me asynchronously pull in scripts. There are others (formatDate: converts datetime string to user's local time, getBrowser: gets browser types based on feature detection, open: opens a link in a new window, etc...)

This core object lets me perform a wide array of tasks... by just calling Core.method... moving nearly all of my javascript code into a .js file which can be cached.

Just out of curiousity, what sorts of common functions do you build into your sites?


Solution

  • I usually add a wrapper for catching any error pages.

    ajaxErrorHandle: function (data, container) {
            if (data.indexOf("Server Error in '/' Application") != -1) {
                container.html(data);
                $('.ajax-loader').hide();
                return false;
            }
            return true;
        }