停止jQuery .html()加载javascript

时间:2015-03-08 23:29:09

标签: jquery ajax smoothstate.js

我正在使用smoothState.js通过ajax预取页面。当它使用html更新页面时,它会再次加载javascript文件。

然后该页面似乎同时运行导致错误。我可以阻止jQuery加载脚本吗?

    /** Run when requested content is ready to be injected into the page  */
    onEnd : {
        duration: 0,
        render: function (url, $container, $content) {
            $body.css('cursor', 'auto');
            $body.find('a').css('cursor', 'auto');
            $container.html($content);
        }
    },

html中的脚本,它在ajax加载后第二次加载

<script src="/js/main.min.js" async defer="defer"></script>

1 个答案:

答案 0 :(得分:1)

如果我理解正确,试试这个:

var stop;


/** Run when requested content is ready to be injected into the page  */
onEnd : {
    duration: 0,
    render: function (url, $container, $content) {
        // the id #content must be after script tag in your page
        if(stop) $content = jQuery($content).find('#content').html(); 
        $body.css('cursor', 'auto');
        $body.find('a').css('cursor', 'auto');
        $container.html($content);
        // after first request enable stop variable
         stop = true;
    }
},

html:

<script src="/js/main.min.js" async defer="defer"></script>

<div id="content">.........</div>