Several major bugs in executing Javascript in IE Kernel

Source: Internet
Author: User

In the script writing process, there are several major bugs that have caused me to find the answer for a long time. I want you to avoid detours.

Note: IE versions earlier than 6.0 will appear, and 7.0 has not been tested

 

I. Converting text into numbers. parseint has a serious bug in IE.
Alert (parseint ("8") → the execution result is 0.
Solution: change to parsefloat ("8"). tofixed (0) → retain the number of digits after the decimal point, that is, the integer

 

Ii. Incorrect execution of the document. getelementbyid Method
Document. All is not supported in other browsers, so document. getelementbyid is the most used. However, I found that when the control ID is the same as the name, the document. getelementbyid error occurs. For example:
<Input id = "txt_name01" name = "txt_name">
<Input id = "txt_name" name = "txt_name">
Alert (document. getelementbyid ("txt_name"). Id) execution result is "txt_name01"
It can be inferred that in IE, document. getelementbyid is first searched by name from top to bottom. It is searched by ID only when the control is not found.
Solution: use another method instead.
Document. getobjectbyid = function (ID)
{
If (document. All)
{
VaR OBJ = Document. getelementbyid (ID );
If (OBJ = NULL) {return NULL ;}
If (obj. ID = ID) {return OBJ ;}
OBJ = Document. getelementsbyname (ID );
For (VAR I = 0; I <obj. length; I ++)
{
If (OBJ [I]. ID = ID) {return OBJ [I];}
}
Return NULL;
}
Else {return document. getelementbyid (ID );}
}

 

3. Set the drop-down node to invalid through innerhtml of the select Node
When two select drop-down boxes need to be copied, the quickest way is to use innerhtml, but I have nothing to say about the IE bug. For example, copy the drop-down node of ddl_dept to ddl_dept02.
<Select id = "ddl_dept">
<Option> 01 </option>
<Option> 02 </option>
</SELECT>
<Select id = "ddl_dept02" style = "width: 20px">
<Option> 03 </option>
</SELECT>

Function clonenode ()
{
VaR d1 = Document. getelementbyid ("ddl_dept ");
VaR D2 = Document. getelementbyid ("ddl_dept02 ");
// D2.innerhtml = d1.innerhtml; this sentence should be the fastest, but it is not valid, so I have to change it.
D2.innerhtml = ""; // This sentence is acceptable. It is speechless.
VaR Re =/(</SELECT>)/GI;
VaR T = d2.outerhtml. Replace (Re, ""); // remove it first </SELECT>
D2.outerhtml = ot + d1.innerhtml + "</SELECT>"; // Add the combined content.
}

 

The above Code only appears in the IE kernel browser, Firefox does not have the above problem

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.