1

I'd like to create a custom .js file for my website so that I do not have to keep linking it within my webpages, my code works when used inline .

Please could someone tell me why my styles.js page wont link? I'm thinking that my code is wrong?

The current page is 'styles.js', the link I use is:

$(document).ready(function() {

            $('#nav').affix({
      offset: {
        top: 90
      }
}); 

$('#sidebar').affix({
      offset: {
        top: 17
      }
}); 

        });


$(document).ready(function() {
    $(window).scroll(function() {
        if ($(this).scrollTop() > 50) {
            $('#back-to-top').fadeIn();
        } else {
            $('#back-to-top').fadeOut();
        }
    });
    // scroll body to 0px on click
    $('#back-to-top').click(function() {
        $('#back-to-top').tooltip('hide');
        $('body,html').animate({
            scrollTop: 0
        }, 800);
        return false;
    });
    $('#back-to-top').tooltip('show');

});
Blazemonger
  • 90,923
  • 26
  • 142
  • 180
etwestlake
  • 11
  • 2
  • 5

1 Answers1

0

A few things to check first:

  1. Check your styles.js file is actually being included, make sure you have no HTML errors when including it and that your file path is correct.

  2. Make sure styles.js is included further down in the source code than jQuery.

If both of the above seem OK, it could be that something is overwriting $ before your code is ran, try wrapping your code in this:

(function($) {
    // Your code here
})(jQuery);

This will force $ to be jQuery within the scope of the function.

Scoots
  • 3,048
  • 2
  • 21
  • 33
  • Thank you so much, this works great now! The linking was fine, thinking that there was maybe one too many '});' in the last part. – etwestlake Mar 03 '17 at 10:16