-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();
 });
});
dumbass
27.2k4 gold badges43 silver badges77 bronze badges
asked May 6, 2014 at 20:52
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 ?

answered May 6, 2014 at 20:55
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);
answered May 6, 2014 at 20:56

1 Comment

its saying Uncaught ReferenceError: $body is not defined

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.