SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Addict skunkbad's Avatar
    Join Date
    Apr 2008
    Location
    Temecula, CA
    Posts
    272
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    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:

    Code:
    $(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.

  2. #2
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,526
    Mentioned
    83 Post(s)
    Tagged
    3 Thread(s)
    The following should work:

    Code javascript:
    $(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:


    Code javascript:
    $(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:


    Code javascript:
    $(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.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  3. #3
    SitePoint Addict skunkbad's Avatar
    Join Date
    Apr 2008
    Location
    Temecula, CA
    Posts
    272
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by pmw57 View Post
    The following should work:

    Code javascript:
    $(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:


    Code javascript:
    $(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:


    Code javascript:
    $(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.
    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?

  4. #4
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,526
    Mentioned
    83 Post(s)
    Tagged
    3 Thread(s)
    Quote Originally Posted by skunkbad View Post
    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?
    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".
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  5. #5
    SitePoint Addict skunkbad's Avatar
    Join Date
    Apr 2008
    Location
    Temecula, CA
    Posts
    272
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by pmw57 View Post
    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.

  6. #6
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,526
    Mentioned
    83 Post(s)
    Tagged
    3 Thread(s)
    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)
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •