Home > Web Front-end > JS Tutorial > body text

How to determine whether jquery is loaded?

青灯夜游
Release: 2020-11-30 14:20:23
Original
2447 people have browsed it

Method: 1. Use the "if (jQuery){//jQuery loaded} else{//not loaded}" statement to judge; 2. Use "if(typeof jQuery=='undefined'){ //jQuery is not loaded} else{//Loaded}" statement to judge.

How to determine whether jquery is loaded?

The operating environment of this tutorial: windows7 system, jquery3.5 version. This method is suitable for all brands of computers.

Related recommendations: "jQuery Video Tutorial"

After the current webpage loads jQuery, the jQuery() or $() function will be defined, so check whether jQuery has been There are the following 2 methods for loading:

Method 1:

if (jQuery) { 
// jQuery 已加载 
} else { 
// jQuery 未加载 
}
Copy after login

Method 2:

if (typeof jQuery == 'undefined') { 
// jQuery 未加载 
} else { 
// jQuery 已加载 
}
Copy after login

Remarks:
Above we check whether the jQuery function has been defined, this is A relatively safe and reliable method, because after you load jQuery.js, you may load prototype.js or mootools.js, etc., in which the $() function may be redefined, and it will be inaccurate to detect whether the $() function exists. .

We usually load jquery. If the bandwidth and speed are not very good, you can consider citing a third-party jquery

<script src="//apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js"></script>
<script type="text/javascript">
  if (typeof jQuery == &#39;undefined&#39;) {
    document.write(unescape("%3Cscript src=&#39;/skin/mobile/js/jquery.min.js&#39; type=&#39;text/javascript&#39;%3E%3C/script%3E"));
  }
</script>
Copy after login

or

<script src="http://lib.sinaapp.com/js/jquery11/1.8/jquery.min.js"></script>
<script>window.jQuery || document.write(unescape("%3Cscript src=&#39;/skin/mobile/js/jquery.min.js&#39; type=&#39;text/javascript&#39;%3E%3C/script%3E"))</script>
Copy after login

One is from Baidu and the other is from Sina Yes, it is not recommended to use Google, Microsoft and other foreign ones. Domestic access is not as fast as domestic access because it is easy to be forced.

For more programming-related knowledge, please visit: Introduction to Programming! !

The above is the detailed content of How to determine whether jquery is loaded?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!