Use CloneNode to share _javascript skills under IE

Source: Internet
Author: User
Tags script tag
CloneNode is a method on the HtmlElement prototype chain that creates a copy of the specified DOM node, accepts a Boolean parameter Include_all, and if Include_all is set to True, the copy takes all the child nodes of the specified node.

However, the script tag is also a DOM node, CloneNode is still valid, after the test of the browser (especially IE) on the performance of CloneNode inconsistent, the main phenomenon is the following two kinds:

IE, at least IE8 and below, when a node is CloneNode, if the node contains a script node, the script content "might" be executed again.
Non-IE browser, cloneNode a node, the script content of the contained scripts node will not be executed again.
I am satisfied with the browser performance other than IE, and the needle for IE "is possible", also divided into the following two kinds of situations:

If a script node is cloneNode, it will not be executed again, whether the node is an outer chain script or an inline script.
If you cloneNode a different node, the embedded script contained under that node will not be executed, and the included outer chain script will be executed again.
Here is a demo to reproduce the problem of IE under the CloneNode.

See here, are you going to get dizzy? The solution is simple, regardless of what browser, before CloneNode, the target node all the script tag can be removed, because the script has been executed, the removal of its label does not affect the following:

Copy Code code 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 we use CloneNode (true), we must be mindful: are all child nodes in the replicated node needed? Try to do not need to kill, to avoid negative effects, for example, if the replication div contains an IFRAME, and the IFRAME page has script parent.xxx ..., then the IFRAME in the script will be again, the IFRAME The page itself is not a problem (nor necessarily), but because it operates parent, the impact of this parent is difficult to measure. The solution is after CloneNode, the copy contained in the IFRAME to kill, of course, if the plot needs, the IFRAME can not be killed, on the IFRAME page of the script to make their own judgments.

In addition, the CloneNode target node contains link tag, this estimate will have some effect, I did not do experiments, if useless, but also removechild, Ehin.

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.