-1

This is a script I have on my page and for some reason I get this error in the console. Here is the error. "Uncaught SyntaxError: Unexpected token ) "

!function ($) {
    //=================================== scroll  ===================================//

$body.scrollspy({
      target: '#navbar-main',
      offset: navHeight
    });

    $window.on('load', function () {
      $body.scrollspy('refresh');
    });

    $('#navbar-main [href=#]').click(function (e) {
      e.preventDefault();
    });


});
1
  • you have an extra ) at the end Commented May 6, 2014 at 20:54

2 Answers 2

3
!function ($) {

});

is a strange pattern to use, and it's not valid, it should be

jQuery(function($) {

});

If you're trying to create a DOM ready handler.
If you just need an IIFE you could do

!function($){ 

}(jQuery);

which looks like what you're trying to use here ?

Sign up to request clarification or add additional context in comments.

Comments

2

Don't write this:

!function ($) {

});

Use this :

$(function() {

});

or this

$(document).ready(function() {

});

If you want to hide all code in anonymous function , the syntax is as following:

(function($) {
    $body.scrollspy({
      target: '#navbar-main',
      offset: navHeight
    });

    $window.on('load', function () {
      $body.scrollspy('refresh');
    });

    $('#navbar-main [href=#]').click(function (e) {
      e.preventDefault();
    });


 })(jQuery);

1 Comment

its saying Uncaught ReferenceError: $body is not defined

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.