Summary of jQuery is not defined Error Causes and Solutions

  • 2021-08-05 08:13:02
  • OfStack

There are usually several solutions to this situation:

1: See if jquery files are imported

Even if the introduction of the file is not through a number of downloaders such as the whole station software download, there will be problems, it is recommended to use Thunderbolt to the official download.

2: Inquire whether the path is wrong, you can click js file path in the page source code.

Sometimes js will automatically add some paths, resulting in path errors

3: JS is an interpretive language, is executed according to the label reference block order, $is the jQuery generated object, need to use, must put jquery. js file in front of JS using it.

Put the jQuery library before the JavaScript script that depends on jQuery, and put the code into document. ready to ensure that DOM is loaded.


<script type="text/javascript" src="path/to/jquery.min.js"></script>
<script type="text/javascript">
 $(document).ready(function() {
  // Depend on jQuery Code of 
 });
</script>

4. Sometimes we use js of cdn of the third party to load jQuery of CDN and fail or time out

An jQuery undefined error occurs when an CDN that provides jQuery fails to load jQuery, or when the browser times out to load the jQuery file due to network problems.

Solution: Mount the jQuery file on your own Web site as a standby, and if CDN fails to load jQuery, use your own Web site to host jQuery. In this way, most users can still speed up their access through CDN, and mistakes can be avoided when CDN goes wrong at 1 denier.


<script src="https://cdn.staticfile.org/jquery/3.1.1/jquery.min.js"></script>
<script> 
window.jQuery || document.write('<script src="/js/jquery.min.js"><\/script>'))
</script>

Is it downloaded through some software such as downloader of the whole station, there will be problems, and it is recommended to download it officially with Thunderbolt.


Related articles: