Jan 20 2009

jQuery 1.3

Posted by German Rumm in javascript

This is probably an old news, but jQuery 1.3 has been released January 14.

Now jQuery includes what was previously known as livequery plugin. Now you can program the future :) Meaning you can attach events to elements that are not in the page (yet). I remember hard time I had (when I didn’t know about livequery) when doing AJAX-pagination for comment moderation module. It was like:

  1. Attach click events to delete links, to hide links, to pager links etc…
  2. Once the new page is loaded, attach click events again to delete links, to hide links …

Now it’s

('a.delete').live('click', deleteComment);
('a.screen').live('click', screenComment);

and that’s it – all future elements will have events attached to them automatically.

jQuery also moved their selector engine to separate object. Now it’s called Sizzle and is completely standalone.

And jQuery is now much faster (and that’s always a good thing). View the graphs on release page and see for yourself.

Oct 24 2008

Looking for jQuery sIFR w/ transparent background support

Posted by German Rumm in javascript

Need jQuery sIFR for a project I am currently working on. Found 2: one by Gilles van den Hoven and another by Jonathan Neal. Liked the second one – documentation, plenty of options, custom callbacks and this one is based on a wonderful flash plugin by Luke Lutman.

Unfortunately both don’t support transparency nor custom background, only background color. At least I couldn’t make it work. wMode=”transparent” didn’t help, setting bgColor to empty(“none”/”transparent”) didn’t work as expected. Will have to look into .fla file, but for that I have to boot into my Windows, and I so-o-o don’t want to do it :(

P.S. sIFR3 Alpha doesn’t support transparent backgound neither.

Apr 12 2007

Safe JSON parsing

Posted by German Rumm in javascript

Read “Mastering JSON” article

Highly recommended reading, even for those who are familiar with JSON.

There’s a very elegant solution to safe JSON parsing – anonymous function the “extends” String.prototype to include parseJSON() method (which will be part of the next Javascript specification due to be released in 2008)

Here it is:

(function (s) {
  // This prototype has been released into the Public Domain, 2007-03-20
  // Original Authorship: Douglas Crockford
  // Originating Website: http://www.JSON.org
  // Originating URL    : http://www.JSON.org/JSON.js
 
  // Augment String.prototype. We do this in an immediate anonymous function to
  // avoid defining global variables.
 
  // m is a table of character substitutions.
 
  var m = {
    '\b': '\b',
    '\t': '\t',
    '\n': '\n',
    '\f': '\f',
    '\r': '\r',
    '"' : '"',
    '': ''
  };
 
  s.parseJSON = function (filter) {
 
    // Parsing happens in three stages. In the first stage, we run the text against
    // a regular expression which looks for non-JSON characters. We are especially
    // concerned with '()' and 'new' because they can cause invocation, and '='
    // because it can cause mutation. But just to be safe, we will reject all
    // unexpected characters.
 
    try {
      if (/^("(\.|[^"\n\r])*?"|[,:{}\[\]0-9.\-+Eaeflnr-u \n\r\t])+?$/.
        test(this)) {
 
          // In the second stage we use the eval function to compile the text into a
          // JavaScript structure. The '{' operator is subject to a syntactic ambiguity
          // in JavaScript: it can begin a block or an object literal. We wrap the text
          // in parens to eliminate the ambiguity.
 
          var j = eval('(' + this + ')');
 
          // In the optional third stage, we recursively walk the new structure, passing
          // each name/value pair to a filter function for possible transformation.
 
          if (typeof filter === 'function') {
 
            function walk(k, v) {
              if (v && typeof v === 'object') {
                for (var i in v) {
                  if (v.hasOwnProperty(i)) {
                    v[i] = walk(i, v[i]);
                  }
                }
              }
              return filter(k, v);
            }
 
            j = walk('', j);
          }
          return j;
        }
      } catch (e) {
 
      // Fall through if the regexp test fails.
 
      }
      throw new SyntaxError("parseJSON");
    };
  }
) (String.prototype);
// End public domain parseJSON block
Mar 24 2007

swfIR flicker fix

Posted by German Rumm in flash, javascript

A while ago I wrote about swfIR and the “flicker” bug it has. Several people came to that post through Google looking for a fix, so I decided to write one.

What’s the problem

When page loads, images disappear for a fraction of second before replacement. That’s kinda annoying. Go and check an example on swfIR web-site to see what I mean.

How the fixed version looks like

View fixed version

The Fix

You can download modified swfir.js here or manually change your swfir.js.

Read entire article.