0

Edit 3/02/12: Found another solution that worked by itself without the need for any other functions; see my own answer below.


I just don't see what's wrong here and have had no luck with the answers to all the other questions titled "SyntaxError: Expected token '}'"

I'm working with a sticky div function that keeps <div class="sticky"> always visible on window scroll.

jQuery 1.7.1 is included by WordPress and loads before the two functions below. All functions load, according to Dev Tools.

A <div class="sticky">with various html</div> is placed just about the </body> tag.

But I get a SyntaxError: Expected token '}' error.

This is in header:

$(document).ready(function(){
    $('.sticky').jsticky({
        marginTop: 0,
    });

This main function is included above it:

(function($){
    $.fn.extend({ 
        //plugin name - jsticky
        jsticky: function(options) {

            //Settings list and the default values
            var defaults = {
                marginTop: 0
            };

            var options = $.extend(defaults, options);

            return this.each(function() {
                var o = options;

                //Assign current element to variable, in this case is UL element
                var obj = $(this);   


    var offset = obj.offset();
    var topOffset = offset.top;
    var marginTop = obj.css("marginTop");
    var marginTopNum = parseInt(marginTop);

    var diff = topOffset - marginTopNum;


    var offset = obj.offset();
                var topOffset = offset.top;
                var leftOffset = offset.left;
                var marginTop = obj.css("marginTop");
                var marginLeft = obj.css("marginLeft");


                $(window).scroll(function() { 
                    var WindowScrollTop = $(window).scrollTop();
                    var scrollTop = WindowScrollTop + o.marginTop;

                    if (scrollTop >= topOffset){

                        obj.css({
                            marginTop: o.marginTop,
                            marginLeft: leftOffset,
                            position: 'fixed'
                        });
                    }

                    if (scrollTop < topOffset){

                        obj.css({
                            marginTop: marginTop,
                            marginLeft: marginLeft,
                            position: 'relative'
                        });
                    }
                });


            });
        }
    });
})(jQuery);
3
  • How are you editing your code? Most (all?) IDEs and quite a few simple text editors (e.g., Notepad++) will show which opening bracket pairs with which closing bracket so you can track down this type of bug. Even jsfiddle's code editor does it. Commented Mar 2, 2012 at 0:04
  • @nnnnnn: I'm using BBEdit, and it has syntax highlighting but it's not an IDE. Commented Mar 2, 2012 at 0:09
  • jslint.com & jshint.com.... handy online syntax validators Commented Mar 2, 2012 at 1:44

3 Answers 3

1

You have a missing }) from the ready callback:

$(document).ready(function(){
    $('.sticky').jsticky({
        marginTop: 0,
    });

It should be:

$(document).ready(function(){
    $('.sticky').jsticky({
        marginTop: 0,
    });
});
Sign up to request clarification or add additional context in comments.

2 Comments

I tried that, but it then throws a "TypeError: 'undefined' is not a function (evaluating '$(document)')" error.
there's no other JS in header, other than jquery 1.7.1 But thanks for the help; I ended up using a different solution that doesn't require an additional long script.
0

The error is exactly correct.
You never closed the outer (function(){.

3 Comments

@nnnnnn: That's why I included the (.
OK. How do I close the function?
@songdogtech: With the } that the parser expects.
0

Found another solution that worked by itself without the need for any other functions:

var name = "#sticky";
var menuYloc = null;

$(document).ready(function(){
menuYloc = parseInt($(name).css("top").substring(0,$(name).css("top").indexOf("px")))
$(window).scroll(function () { 
offset = menuYloc+$(document).scrollTop()+"px";
$(name).animate({top:offset},{duration:500,queue:false});
});
}); 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.