清除缓存后,getscript不起作用

时间:2012-10-10 22:43:55

标签: jquery getscript

我正在使用getscript()为我的图片库加载一些脚本。只要我点击图库,这段代码就会运行:

$(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;
            });
        });
    });
});

首先我转到chrome控制台 - >右键单击网络选项卡 - >清除浏览器缓存 - >刷新页面 - >并点击照片。 我得到警报“soundswaste1”,效果不会运行。图像只是在页面上排成一行。

然后我再次点击“照片”,我同时收到两个警报,每个警告两次。 2个问题:

  1. 为什么清除缓存会阻止第一次加载所有脚本。是因为它们位于不同的路径上吗?

  2. 为什么我第二次收到两次的提醒?

1 个答案:

答案 0 :(得分:1)

我猜galleria.classic.min.js取决于galleria-1.2.8.js 在第一次加载时,你试图一个接一个地加载它们, 但是,确保当加载第二个脚本时,第一个脚本完成加载。 所以在第一个的success上加载第二个,如下所示:

$(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;
        });
    });
相关问题