停止 jQuery .html() 加载 javascript
Stop jQuery .html() loading javascript
我正在使用 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>
如果我理解正确,试试这个:
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>
我正在使用 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>
如果我理解正确,试试这个:
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>