1. last comma
For example, in this Code Section, note that the last comma should be good in terms of Linguistics (this is acceptable for a dictionary similar to the data type in python ). Internet Explorer reports syntax errors, but it is not detailed. You can only scan thousands of lines of code.
<Script>
Var theObj = {
City: "Boston ",
State: "MA ",
}
</Script>
2. the reference of this will change.
Such as this Code:
<Input type = "button" value = "Gotcha! "Id =" MyButton ">
<Script>
Var MyObject = function (){
This. alertMessage = "Javascript rules ";
This. ClickHandler = function (){
Alert (this. alertMessage );
}
}();
Document. getElementById ("theText"). onclick = MyObject. ClickHandler
</Script>
The answer is not "JavaScript rules", as you wish ". When executing MyObject. ClickHandler, this row is red in the Code. The reference of this actually points to the reference of document. getElementById ("theText. It can be solved as follows:
<Input type = "button" value = "Gotcha! "Id =" theText ">
<Script>
Var MyObject = function (){
Var self = this;
This. alertMessage = "Javascript rules ";
This. OnClick = function (){
Alert (self. value );
}
}();
Document. getElementById ("theText"). onclick = MyObject. OnClick
</Script>
Essentially, this is the JavaScript scope issue. If you see it, you will find that there are more than one solution.
3. Identify thieves
Do not use variable names with the same HTML id in JavaScript. The following code:
<Input type = "button" id = "TheButton">
<Script>
TheButton = get ("TheButton ");
</Script>
IE will report an undefined object error. I can only say: IE sucks.
4. Replace the string with only the first match
The following code:
<Script>
Var fileName = "This is a title". replace ("","_");
</Script>
In fact, the result is "This_is a title". In JavaScript, the first parameter of String. replace should be a regular expression. Therefore, the correct method is as follows:
Var fileName = "This is a title". replace (// g ,"_");