1

I've tried all the solutions from other Stackoverflow topics, but I can't my jQuery file to work. I use a Wampserver to test my code and I test inside of multiple browsers (Firefox, Chrome, Opera). Same problem every time.

HTML <script src="js/scripts.js"></script> <script src="js/jquery-3.2.1.min.js"></script>

I've put these lines before my closing <\body> tag

scripts.js`

(function($) {
'use strict';

// holds current image shown
var $currNr = 0;
var links, images;

/**
 * Function to prevent closures; adds click event handler to links
 */
var addEvents = function(nr) {
    // add click event to link
    $(links[nr]).on('click', function(e) {
        showImage(nr);
        e.preventDefault();
    });
};

/**
 * Function to show an image
 */
var showImage = function(nr) {
    // find image
    var $img = $(images[nr]);
    if (!$img) return;

    // find big image
    var $photoBig = $('#photoBig');

    // change
    $photoBig.attr('src', $img.attr('data-src-l'));
    $photoBig.attr('alt', $img.attr('alt'));

    // remember current image number
    $currNr = nr;
};

/**
 * Function to show the next image
 */
var showNextImage = function() {
    if ($currNr != (links.length - 1)) {
        showImage($currNr + 1);
    } else {
        showImage(0);
    }
};

/**
 * Function to show the previous image
 */
var showPrevImage = function() {
    if ($currNr != 0) {
        showImage($currNr - 1);
    } else {
        showImage(links.length - 1);
    }
};

// start scripts
$(window).on('load', function() {
    // find images and links
    links = $('#thumbsmenu li>a');
    images = $('#thumbsmenu li>a img');

    // add link events
    $(links).each(function(nr) {
        $(this).on('click', function(e) {
            showImage(nr);
            e.preventBubble();
        });
    });


    // controls
    $('#lnkFirst').on('click', function(e) {
        showImage(0);
        e.preventDefault();
    });
    $('#lnkPrev').on('click', function(e) {
        showPrevImage();
        e.preventDefault();
    });
    $('#lnkNext').on('click', function(e) {
        showNextImage();
        e.preventDefault();
    });
    $('#lnkLast').on('click', function(e) {
        showImage(images.length - 1);
        e.preventDefault();
    });

    // keyboard
    $(document).on('keydown', function(e) {
        var $code = (e.keyCode ? e.keyCode : e.which);
        switch (event.keyCode) {
            case 37: showPrevImage(); e.preventDefault(); break;
            case 39: showNextImage(); e.preventDefault(); break;
        }
    });

    // play
    var $timer;
    $('#btnPlay').on('click', function(e) {
        if (!$(this).prop('playing')) {
            $(this).val('stop');
            $(this).prop('playing', true);
            $currNr = 0;
            $timer = setInterval(showNextImage, 1000);
        } else {
            $(this).val('start');
            $(this).prop('playing', false);
            clearInterval($timer);
        }
    });

});})(jQuery);`

I use a self-invoking function and use it as parameter but I get a:

ReferenceError: jQuery is not defined

Normally this code should work, that was how I was taught back then. Anyone an idea what could be the real issue here?

3
  • 1
    It looks quite obvious. You're loading jQuery after your main script. Load it before (change the order of the lines) Commented Oct 24, 2017 at 13:45
  • 1
    Place the jquery before your scripts.js Commented Oct 24, 2017 at 13:45
  • 1
    If scripts.js is using jQuery, then you need to include jQuery first... Commented Oct 24, 2017 at 13:45

3 Answers 3

7

You are loading scripts.js before jQuery

Change the following code:

<script src="js/scripts.js"></script>
<script src="js/jquery-3.2.1.min.js"></script>

To:

<script src="js/jquery-3.2.1.min.js"></script>
<script src="js/scripts.js"></script>
Sign up to request clarification or add additional context in comments.

5 Comments

Ha, there you go :)
Thanks! Stupid mistake, forgot that the order does matter in jQuery. It's been a while!
Not only with JQuery. In general if JS scripts depend on each other you always need to order them properly.
@Lor order matters in HTML, in that case.
@JeremyThille The positive side is that I will never forget to do this in the future :)
2

Your script load ordering is wrong. Load jQuery first, then use it.

1 Comment

Thanks for the help!
2

As other people suggested your order is wrong. In order for you to use JQuery in other script files it needs to be fully loaded. Change your HTML like so:

<script src="js/jquery-3.2.1.min.js"></script>    
<script src="js/scripts.js"></script>

1 Comment

Thanks for the quick response!

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.