When we first try to run JavaScript on a web page, it is possible that the Code does not work as expected. At this time, we often receive error messages, which tell us what happened.
There are many reasons why scripts do not run, but some common errors may occur when writing scripts. In fact, if we check these common errors, we may find the cause of the failure of the script or at least correct some errors. Of course, all errors may be small, but once common errors are corrected, other errors are relatively easy to find.
What are the common errors we want to check? How can we locate and fix these common errors? This is what we will talk about in this article.
Large arc with mismatching or misplacement
JavaScript uses {} to include commands. You can use this pair of Large arc statements to include a group of statements. These command statements can be considered as a module. When multiple condition statements or combined loops are used, these modules can be nested with each other. When the layers of these statements are too many, it is very likely that there will be nested errors. Remember that each {symbol should have a corresponding} symbol.
In another case, when we have a large number of code blocks nested, we may lose the code-level path and place the code in the wrong level, finally, the Code cannot take effect as expected.
To correct this type of problems caused by arc mismatch, the simplest way is to get the code and mark the corresponding pairs with different high brightness tags. Mark the first {symbol with High Brightness at the beginning of the Code. Then, locate the same level} along the code, and mark the High Brightness with the same color. Then Mark the next {with another color with high brightness, and mark the next} with the same color. In this way, keep the same level of {} color to avoid errors.
Incorrect matching quotation marks
With HTML and JavaScript, we can use (") or ('). When the HTML code contains a document. write statement, use "" in HTML and ''In JavaScript ''. If this is done, such as document. write ('<a href = "mypage.htm">') is not displayed, because the quotation marks are completely nested in single quotes.
There are two possible cases. We may accidentally enclose a single quotation mark, leading to the end of the subsequent statement as part of the text string, so the Code cannot take effect. In another case, you may need to include a true single quotation mark in the text. To prevent a single quotes with text from ending the text, you must add a backslash before it, such as document. write ('Let/'s go .').
If you accidentally close the quotation marks or forget to close the quotation marks, the code will become invalid. We can check such errors through code printing and three High Brightness labels. At the beginning of the text, you need to avoid ambiguity, and the end of the text can be highlighted with high brightness.
Parentheses are missing in the condition sentence.
When writing a conditional statement, parentheses must be used on the condition. When multiple conditions exist, each condition must be enclosed in an arc. In practice, it is easy to forget to include conditions in the arc, and when there are too many nested layers, it is easier to mistake the hierarchy.
The solution to this problem is similar to the solution to the error matching of the Large arc. You only need to mark different colors for different levels of arc.
Missing semicolon
Each JavaScript statement should end with a semicolon. Sometimes, every statement in the JavaScript code we see is arranged separately, while the semicolon is separated from the Code. However, if we need to reorganize the code, if you leave the two statements in the same row and forget to add a semicolon to them, the code will not take effect.
The simplest way to solve this problem is to reformat the code so that each code appears in a separate line and make sure that each line ends with a semicolon. In this way, the code can be combined at any time without affecting its normal operation.
Incorrect uppercase match
JavaScript is very sensitive to Case sensitivity. This means that myvar and myVar are two different variables. To avoid variable errors caused by Case sensitivity, we recommend that you use the standard method to name variables. For example, use the thisIsMyVariable format or this_is_my_variable format. In this way, we can effectively distinguish variables with different cases. When looking for errors, the conspicuous format features are also conducive to the use of accurate designated search.
If the names of the two variables are the same, a problem may occur, that is, not all browsers can correctly execute the code, and the variables with only the similarities and differences between case and case may be the same variables, the code with incorrect capitalization cannot be run in Some browsers.
Reference objects before object Loading
The JavaScript code in the page header is not included in the function that is run before the page is loaded. The JavaScript of the page content is loaded as part of the page. The JavaScript called from the loading event processor runs after the page is loaded. The function runs when JavaScript code is called. The event processor runs when the related event is triggered.
If JavaScript is accessing the content on the Web page through the Document Object Model, the objects you are referencing must have been loaded before reference. An error occurs when you reference an object that has not been loaded. To correct this problem, we should ensure that no JavaScript Object from the page header is referenced unless it is included in the function called after the page is loaded. The reference to some JavaScript objects on the page content depends on the order in which JavaScript and objects get loading speeds. It is best to transfer the processing status of the object to the page loading event processor.
Reserved Words used as variable names
For JavaScript, some words have special meanings. These reserved words cannot be used as variables. If we accidentally use these reserved words as variable names, the processing results will be unsatisfactory.
The best solution is to familiarize yourself with the reserved word list. If we name variables according to appropriate naming standards, we can avoid using reserved words as variable names because standard naming conventions prevent us from using these reserved words.
Parameters lost during function call
When defining a function, we define the number of parameters. When writing code to call a function, we need to transmit the expected parameters. It is relatively easy to check the number of parameters or check the time when parameters are received after the code sends a call request. If the parameter has been given a reasonable name, it should also clearly indicate which parameters need to be transferred to each parameter.
In this case, if we decide to add additional parameters to the function, the problem may occur. All calls to the function now need to be updated to transfer this additional parameter. If a function is an external JavaScript code linked to many pages, it is easy to omit calls to the function from one page to another.
Unsupported Document Object Mode
Most browsers support the W3C standard Document Object Mode. Assuming that we are using one of these browsers, the scripts can run normally when we test the script edited to use the standard DOM in the browser. However, it is possible that the browser version of page visitors is outdated. To make the script support this part of the visitor, you need to edit the script into a cross-browser DOM.
Many old browser scripts can be downloaded from the Internet. Of course, new scripts can also be downloaded. To correct this error, we need to rewrite the script to use the standard DOM or cross-browser DOM. If the script involves document. all or document. layers, we can identify that it uses the old DOMs.