Superfish and if block in options

I want to pass option values to superfish only if the browser is > IE6, so I thought to do this:


$(document).ready(function() {
	$('ul.sf-menu').superfish({
		if ($.browser.msie && $.browser.version > 6){
			animation:   {height:'show'},
			speed:       'fast'
		}
	});

});

but it isn’t right. It breaks the javascript, and doesn’t run at all. Anyone have some clues or answers for me? I don’t use jquery or javascript enough to figure it out.

You may find some of Douglas Crodkford’s work to be of use then.

A Survey of the JavaScript Programming Language

Crockford on JavaScript – Act III: Function the Ultimate (video)

Yes, this worked great. One more unrelated question. I have bought and read a jquery book, but it didn’t teach me enough to actually do something simple like this. How did you learn this?

The following should work:


$(function() {
    var superfishOptions = {};
    if ($.browser.msie && $.browser.version > 6) {
        superfishOptions = {
            animation: {height:'show'},
            speed: 'fast'
        };
    }
    $('ul.sf-menu').superfish(superfishOptions);
});

We can use a ternary statement instead as well:


$(function() {
    var superfishOptions = ($.browser.msie && $.browser.version > 6)
        ? {
            animation: {height:'show'},
            speed: 'fast'
        }
        : {};
    $('ul.sf-menu').superfish(superfishOptions);
});

And even move the ternary in to the superfish declaration:


$(function() {
    $('ul.sf-menu').superfish(
        ($.browser.msie && $.browser.version > 6)
        ? {
            animation: {height:'show'},
            speed: 'fast'
        }
        : {}
    );
});

But I think that may agree that the first code example is the easiest to understand.

I learned things like this through large amounts of exposure to javascript, reading lots of books, writing lots of code, and learning from others.

Libraries such jQuery are very good at what they do, however such libraries only cover a small subset of what’s possible. I wouldn’t want them to try and improve on that either.

This helps to explain one of the common things that is heard around here a lot, which is “Learn javascript first, and then learn about libraries like jquery afterwards”.

When I learned php, I went through at least 5 books before I felt like I knew it well. There was that 1 book that really made a big difference, and I wish there was that for javascript. So far I’ve only read 1 javascript book and 1 jquery book, but they were incomplete, and rather basic in both cases. I think what confuses me most is the syntax of many jquery plugins, and what I see as a “new style” or “advanced” javascript. Is it object oriented? Maybe it’s been around for a long time, but I just can’t wrap my head around this advanced stuff, mostly because of the weird syntax. Just like php has procedural and OOP, I’m guessing that the javascript that I don’t understand is of a different nature, and I just need the right book for the job. I do understand the need to learn the language before the framework/library. I am willing to do it.