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:
do this:
$("#item_id > a")
$("#item_id > ul > li > a'")
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:
Result:
ReferenceError: unreferencedVariableName is not defined
so much to remember…
if (unreferencedVariableName) { alert('Impossible! an unreferenced variable cannot return true. In fact it cannot return at all!'); }
/* 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!') }
comparison of Fixnum with String failed
Where the error occured
Rails 2.1.2 / bundle_fu / Javascript fileWhy 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.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');
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.
imagemap rollovers with area shape="poly\
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.