每当我单击对包含名为 status.js
的 JS 文件的其他页面进行 AJAX 调用的按钮时,每次进行 AJAX 调用时都会加载它。
我需要它只加载一次,否则如果加载多次,那么它应该只执行一次。
最佳答案
我可以看到你这样做有两种方式:
将代码包装在变量的“status.js”内。
if (!status || status === undefined) { var status = function() { // code here } }
或者使用 jQuery 加载函数代替 ajax 并仅加载您需要的页面正文 ( https://api.jquery.com/load/ ):
$( "#result" ).load( "ajax/test.html #container" );
有了这个,您可以在内部构建一个开关,在第一次单击按钮时加载整个页面,但在第二次单击时加载除脚本之外的所有内容
firstClick = true; if(firstClick) { // load entire page firstClick = false; } else { // load everything but the scripts }
关于javascript - 使用多个依赖项加载仅进行一次 AJAX 调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29435826/