JavaScript-function reports Uncaught ReferenceError: XX is not defined

In js development, many people encounter similar problems and cannot find a solution;

Uncaught ReferenceError: XX is not defined

Here are the main solution steps

method/step

1. If this error occurs, the most likely reason is that the calling sequence of the referenced js is wrong. Re-adjust the reference order to see if it can be resolved?

The reason is that the order of introducing jquery.js is wrong, jquery.js should be placed before other js

 <script src="__ADMIN__/js/jquery.js" language="JavaScript" type="text/javascript"></script>
 <script src="__ADMIN__/js/bilibili.js" language="JavaScript" type="text/javascript"></script>

2. Another reason is that when you reference js, the path is wrong, not the correct file path in your project

3. Another reason is that some values ​​in your js do not exist

4. The JS function is undefined (I believe that this kind of low-level will not be committed by anyone, but it does not rule out that the defined function and the declared function name are inconsistent, or the incoming parameters are inconsistent)

5. Put the Javascript function outside the body and head

When writing javascript functions in html, there are usually three forms:

(1) Internal: in the <body></body> of the Html web page

(2) Internal: in the <head></head> of the Html web page

(3) External: in the external JS file

(1) Written in <body></body>

When the browser loads the Body part of the webpage, it executes the Javascript statement in it, and the output content after execution is displayed on the webpage

<html>
    <head>...</head>
    <body>
        <script type="text/javascript">....</script>
    </body>
</html>

(2) Write in <head></head>

When you don't want to run javascript as soon as the HTML is loaded, but by triggering an event, this makes Javascript usually placed in the <head></head> of the HTML

<html>
    <head>
        <script type="text/javascript">....</script>
    </head>
    <body>
        ...
    </body>
</html>

6. Javascript functions are defined before the object declaration. If your code can run directly, if the object operated by the code is behind the code, some browsers will return an error. Because they load objects in order

As follows: Wrong use of the tag defer attribute

<script src="__ADMIN__/js/jquery.js" type="text/javascript" defer="defer"></script>

see the official description

defer 属性规定是否对脚本执行进行延迟,直到页面加载为止。

Because of the defer attribute, the desired effect was delayed and an error occurred

7. In addition to the above specific errors, you may also encounter similar Uncaught ReferenceError: XXXX is not defined errors. For such errors, you need to check for value passing errors, or some values ​​do not exist

Guess you like

Origin blog.csdn.net/MinggeQingchun/article/details/118712689#comments_27863566