Question

I am using getscript() to load some scripts for my image gallery. As soon as I click on the gallery, this code runs :

$(function () {
$(window).hashchange(function () {
    $(document).ready(function () {
        $('#nav li a').click(function () {
            $('#content-wrap').load(toLoad, showNewContent);

            function showNewContent() {
                if ($('#content-wrap').is(':empty')) {
                    return false;
                } else {
                    if (window.location.hash == "#photos") {
                        alert("soundswaste1");
                        $.getScript("galleria/galleria-1.2.8.min.js");
                        $.getScript("galleria/themes/classic/galleria.classic.min.js", getGalleria);

                        function getGalleria() {
                            alert("soundswaste2");
                            $.getScript("galleria.js");
                        }
                    }
                    $('#content-wrap').fadeIn(500).css("display", "block");
                }
                return false;
            });
        });
    });
});

First I go to chrome console -> right click on network tab -> clear browser cache -> refresh the page -> and click on photos. I get alert "soundswaste1" and the effect doesn't run. The images just line up on the page.

Then I simply click on 'photos' again, and I get both the alerts, each of them twice. 2 questions :

  1. Why does clearing-cache prevents all the scripts from loading the first time. Is it because they are located at different paths?

  2. Why do I get the alerts twice the 2nd time?

Was it helpful?

Solution

i guess galleria.classic.min.js depends on galleria-1.2.8.js on first load, you tried loading both of them one after the other, but that dosent make sure that when the second scripts is loaded the first is done loading. so on the success of the first, load the second, like so:

$(document).ready(function () {
    $('#nav li a').click(function () {
        $('#content-wrap').load(toLoad, showNewContent);

        function showNewContent() {
            if ($('#content-wrap').is(':empty')) {
                return false;
            } else {
                if (window.location.hash == "#photos") {
                    alert("soundswaste1");
                    $.getScript("galleria/galleria-1.2.8.min.js",

                    function () {
                        $.getScript("galleria/themes/classic/galleria.classic.min.js",

                        function () {
                            alert("soundswaste2");
                            $.getScript("galleria.js");
                        });
                    });


                }
                $('#content-wrap').fadeIn(500).css("display", "block");
            }
            return false;
        });
    });
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top