Home >Web Front-end >JS Tutorial >How to detect if jquery is loaded

How to detect if jquery is loaded

coldplay.xixi
coldplay.xixiOriginal
2020-12-24 13:46:123135browse

Method to detect whether jquery has been loaded: 1. Determine whether [jQuery()] is defined, the code is [if (jQuery) { jQuery has been loaded}]; 2. Determine whether [$()] is defined Definition, the code is [if(typeof jQuery == 'undefined'].

How to detect if jquery is loaded

The operating environment of this tutorial: windows7 system, jquery2.1.4 version, DELL G3 computer .

Recommendation:jquery video tutorial

Method to detect whether jquery has been loaded:

After the current webpage loads jQuery, the jQuery() or $() function will be defined, so there are the following two methods to detect whether jQuery has been loaded:

Method 1:

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

Method 2:

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

Remarks:

Above we check whether the jQuery function has been defined, which is a relatively safe and reliable method method, because when you load jQuery.js, you may load prototype.js or mootools.js, etc., which may redefine the $() function, and detecting whether the $() function exists will not Accurate.

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>

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>

Related free learning recommendations: javascript (video)

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

Statement:
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