2016-07-15 93 views
-2

jQuery文件(jquery-1.11.3.min.js)加载多次(4)次。我想我知道这是因为index.html页面中的下面的jquery文件。我不知道的是如何阻止它。jquery文件加载多次

<script type="text/javascript" src="js/jquery-ui.min.js"></script> 
<script type="text/javascript" src="js/jquery.mobile-1.4.5.min.js"></script> 
<script type="text/javascript" src="js/jquery.dataTables.min.js"></script> 

任何建议将非常感激。

enter image description here

enter image description here

我刚刚创建了一个演示页像下面在VS,我可以看到jQuery的1.11.3.min.js的4个实例在VS解决方案的窗口。 我认为这个问题只是在Visual Studio中,似乎浏览器的开发人员工具只显示了jquery-1.xx.js文件的一个实例。 对不起,造成的不便。

<!DOCTYPE html> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
    <title>Demo</title> 

    <script type="text/javascript" src="G5 - KA/js/jquery-1.11.3.min.js"></script> 
    <script type="text/javascript" src="G5 - KA/js/jquery-ui.min.js"></script> 
    <link rel="stylesheet" href="G5 - KA/css/jquery-ui.min.css" /> 
    <script type="text/javascript" src="G5 - KA/js/jquery.mobile-1.4.5.min.js"></script> 
    <link rel="stylesheet" href="G5 - KA/css/jquery.mobile-1.4.5.min.css" /> 
    <script type="text/javascript" src="G5 - KA/js/jquery.dataTables.min.js"></script> 
</head> 
<body> 
    <p>This is just jquery test</p> 
</body> 
</html> 
+4

没有那些加载的jQuery v1.11.3在所有的做任何事情。 –

+1

我们将需要看到更多的页面。在浏览器和查看源上点击它,以确保您看到浏览器实际正在处理的内容。 – n8wrl

+0

在这三个之前添加'jquery'并确保您使用兼容版本。 –

回答

-1

这是最好的方法。

//只有jQuery是没有定义

if (typeof jQuery == 'undefined') { 
if (typeof $ == 'function') { 
    // warning, global var 
    thisPageUsingOtherJSLibrary = true; 
} 

function getScript(url, success) { 

    var script  = document.createElement('script'); 
     script.src = url; 

    var head = document.getElementsByTagName('head')[0], 
    done = false; 

    // Attach handlers for all browsers 
    script.onload = script.onreadystatechange = function() { 

     if (!done && (!this.readyState || this.readyState == 'loaded' || this.readyState == 'complete')) { 

     done = true; 

      // callback function provided as param 
      success(); 

      script.onload = script.onreadystatechange = null; 
      head.removeChild(script); 

     }; 

    }; 

    head.appendChild(script); 

}; 

getScript('http://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js', function() { 

    if (typeof jQuery=='undefined') { 

     // Super failsafe - still somehow failed... 

    } else { 

     // jQuery loaded! Make sure to use .noConflict just in case 
     fancyCode(); 

     if (thisPageUsingOtherJSLibrary) { 

      // Run your jQuery Code 

     } else { 

      // Use .noConflict(), then run your jQuery Code 

     } 

    } 

}); 

} else { // jQuery was already loaded 

// Run your jQuery Code 

}; 
+0

这并不能解释为什么OPs jQuery加载4次。你应该使用JS来加载jQuery。 – putvande

+0

@putvande使用小提琴我们将需要看到更多的页面。 –