Things I learned the hard way XIVII

jQuery When the DOM isn't reacting to your javascript selectors as expected try a little specificity: instead of this:
  $("#item_id > a")
do this:
  $("#item_id > ul > li > a'")

Posted by jeremy 14/02/2010 at 15h41


Javascript Exceptions - testing for true - When is undefined actually undefined?

Javascript has to know what 'it' is before Javascript can tell if 'it' is what you want 'it' to be. Example:
if (unreferencedVariableName) { alert('Impossible! an unreferenced variable cannot return true. In fact it cannot return at all!'); }
Result: ReferenceError: unreferencedVariableName is not defined
/* is as true as the unescapable pain and suffering of being an alive human being  */
if (typeof(cowBoySuckMonkey) === 'undefined') { console.log('suck it!') }


/*   these are false - no if-fu for you!   */
if (typeof(cowBoySuckMonkeee) != 'undefined') { console.log('suck it again!') }
if (!typeof(cowBoySuckMonkeee)) { console.log('suck it for real!') }

/* 'undefined is something I guess - just not what I wanted to test against */
if (typeof(cowBoySuckMonke)) { console.log('suck it nut gobbler!') }

var cowboySuckMonkay = '';
if (typeof(cowboySuckMonkay) != 'undefined') { console.log('suck it empty string!') }
/* no joy */
if (typeof(cowboySuckMonkay)) { console.log('suck it empty string, part duex!') }

/* well, I guess an empty string is false - CaulkGobblin! */
if (cowboySuckMonkay) { console.log('suck it empty string, you still not true!') }

if (cowboySuckMonkay == '') { console.log('suck it empty string, now you true!') }
so much to remember…

Posted by jeremy 18/08/2009 at 15h06


comparison of Fixnum with String failed

Where the error occured

Rails 2.1.2 / bundle_fu / Javascript file

Why the fu…?

A javascript files that ends with a line commented out. -1 is being compared to an empty string.

Fix

Add a blank line at the end of the Javscript file(s) that end with commented lines.

Posted by jeremy 15/08/2009 at 07h45


snipdbits

I've been rocking the Javascript one liners lately. Syntax like this is way not noob friendly.
// both are the same
  if ( 0 == 0 && 0 === 0)
    alert('0 is equal to 0, and 0 is the same value and object type as 0');


  if ( 0 == 0 && 0 === 0)  alert('0 is equal to 0, and 0 is the same value and object type as 0');

Posted by jeremy 06/08/2009 at 08h28


jrails link_to_function hackaway

<%= link_to_function( "insert it", :id => 'foo') do |page| 
      partial = escape_javascript(render :partial => "my_partial", :object => Object.new)
      page <<  "$('#my_div').append('#{partial}')"
    end %>

Sweetness.

Posted by jeremy 01/04/2009 at 01h56


imagemap rollovers with area shape="poly\

[UPDATE:Now with Prototype]

dilemma: an image map with irregularly shaped link areas that need to have a separate hover state for each link/area. solution: javascript, css classes and advantageous id/class naming. stats: 5 files = 2 gifs; 1 css; 1 js; 1 html Using part sliding doors and part unobtrusive javascript I ground out some simple code to do the business of making an image map with link hover states. *view example* (opens new window) get the code First things first: Let the *DOM* be your guide.

Posted by jeremy 20/12/2005 at 03h12