SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Enthusiast
    Join Date
    Jul 2004
    Location
    Southern Georgia
    Posts
    26
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    jQuery: Novice to Ninja => I'm not understanding why this does/doesn't work

    On page 77 of jQuery: Novice to Ninja they have the following excerpt;
    var $window = $(window).
    $navigation = $("#navigation");
    ...

    What I cannot for the life of me is figure out why it ONLY works if I have $window instead of window.

    // This would not work
    var window = $(window),
    navigation = $('#navigation');

    window.scroll(function() {
    navigation.css('top', $(document).scrollTop());
    });

    // This will work
    var $window = $(window),
    navigation = $('#navigation');

    $window.scroll(function() {
    navigation.css('top', $(document).scrollTop());
    });

    Can someone help me understand why? I've read where preceding a var with the $ is common practice if the variable contains a jQuery object, but why will the window object only work if I precede the variable with the $?

    I have a JSFiddle here: http://jsfiddle.net/ryentzer/3hAvm/

    Scratching head,
    Rick

  2. #2
    Community Advisor bronze trophy
    fretburner's Avatar
    Join Date
    Apr 2013
    Location
    Brazil
    Posts
    1,389
    Mentioned
    45 Post(s)
    Tagged
    12 Thread(s)
    Hi Rick,

    window is a built-in browser object, so I would suspect that the browser won't allow you to reassign it.

  3. #3
    SitePoint Enthusiast
    Join Date
    Jul 2004
    Location
    Southern Georgia
    Posts
    26
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks, that is exactly what it was. I was getting hung up, thinking that the $ in front of the variable name was causing some jQuery magic. What I should have done is tested with a random name to clue me in. I couldn't see the forest through the trees so-to-speak.


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
  •