Home > Web Front-end > JS Tutorial > JavaScript compatibility and debugging skills

JavaScript compatibility and debugging skills

高洛峰
Release: 2016-12-06 09:42:40
Original
1237 people have browsed it

Regarding JavaScript compatibility, the laziest way is to use jQuery’s tool function. Try not to use functions such as ECMAScript, because many browsers will report a function not found error. Listed below are some JavaScript problems encountered during the development process.

1. The parameter list contains multiple commas.

  $.ajax({}) method, you are very familiar with it, but there is a small thing to pay attention to in IE. If you add a comma to the last one when splicing the parameter list, then there is no doubt that all the parameters will be lost in IE. JS is invalid.

The following error is reported during debugging:

Missing identifier, string or number

data: {
 S_Id: Subject_Id,
 level: $("#addKey").attr("lang"),  --如果写上这个逗号,IE会报错,火狐谷歌正常。
},
Copy after login

2. The difference between var str; and var str=""

<script>
var Str1;
for (var i = 0; i < 3; i++)
{
  Str1 += "xxx"
}
alert(Str1);
var Str2 = "";
for (var i = 0; i < 3; i++)
{
Str2 += "xxx"
}
alert(Str2);
</script>
Copy after login

The two output results are as follows :

The first time:

JavaScript compatibility and debugging skills

The second time:

JavaScript compatibility and debugging skills

A variable is undefined if it is not assigned a value when it is defined. Adding a string is undefined + "the string to be added". If you assign a value to an HTML element like this. Undefined will also be displayed. You know how to pay attention to it.

3. Debugging IE cached JS

When I was debugging today, I found that IE8’s JS cache is very, very domineering. Refreshing and clearing the cache have no effect. It makes the refresh until the left mouse button is almost broken, but it still doesn't refresh.

 Now let me talk about a special trick, which is to add new Date() at the end when introducing JS. This way you don’t have to refresh so hard.

4. JS determines whether the function/variable exists, and if it exists, call

 javascript When null, undefined, 0, "", and false are used as conditions of if, they are considered false.

 So for variables, if we define a variable var str = ""; based on this judgment, it will still return false.

//是否存在指定函数
 function isExitsFunction(funcName) {
 try {
 if (typeof(eval(funcName)) == "function") {
 return true;
 }
 } catch(e) {}
 return false;
 }
 //是否存在指定变量
 function isExitsVariable(variableName) {
 try {
 if (typeof(variableName) == "undefined") {
 //alert("value is undefined");
 return false;
 } else {
 //alert("value is true");
 return true;
 }
 } catch(e) {}
 return false;
 }
Copy after login



Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template