Notes for using clonenode in IE

Source: Internet
Author: User

Clonenode is a method on the htmlelement prototype chain. It is used to create a copy of a specified Dom node. It accepts a Boolean parameter include_all. If include_all is set to true, the copy carries all the child nodes of the specified node.

However, the script tag is also a DOM node, and clonenode is still valid for it. The results of clonenode execution by different browsers (especially IE) are inconsistent according to the actual test. The main phenomena are as follows:

IE, at least IE8 and earlier. If a node named clonenode contains a Script node, the script content of the Script node may be executed again.
For a non-IE browser, the script content of a node named clonenode is not re-executed.
I am very satisfied with the performance of browsers other than IE, but there are two possible situations for IE described above:

If clonenode is a script node, no matter whether it is an external link script or an embedded script, it will not be executed again.
If clonenode is another node, the embedded scripts under the node will not be executed, but the external link scripts will be executed again.
Here is a demo that reproduce the clonenode problem in IE.

Are you going to be dizzy? The solution is very simple. You don't need to worry about the browser. Before clonenode, you can remove all the script labels under the target node. Because the script has been executed, removing its labels will not affect the operation, as follows:

CopyCode The Code is as follows: function clonenode (DOM ){
VaR scripts = Dom. getelementsbytagname ("script ");
For (VAR I = scripts-1, s; I> = 0; I --){
S = scripts [I];
S. parentnode. removechild (s );
}
Return Dom. clonenode (true );
}

Therefore, when using clonenode (true), we must consider whether all the subnodes In the copied node are required? Try to eliminate unnecessary ones to avoid negative effects. For example, if the copy Div contains IFRAME, And the IFRAME page contains the script parent. xxx ..., These scripts in IFRAME will inevitably be re-executed. The IFRAME page itself is fine (and not necessarily), but because it operates the parent, the impact of this parent is hard to estimate. After clonenode is used, the IFRAME contained in the copy is killed. Of course, if the IFRAME cannot be killed as required by the plot, the script on the IFRAME page is used for self-determination.

In addition, if the clonenode target node contains the link tag, this estimation will also have some impact. I did not perform the experiment. If it is useless, it would also be removechild.

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.