Dynamic Loading of js, css, and other files across iframe implementation

Source: Internet
Author: User

1. dynamically load js and css files (using native js and jquery)

Iframe structure:
Frame0 (parent)
Frame2 (sub)
Frame3 (sub)

In frame2, the trigger event dynamically loads js, css files, and dom elements to frame3?

* The same level can be called. You can use the child-parent-child method to call the same level.
Parent. parentFram ("This method is calling other sub-farme ");

1. jquery append ()
Copy codeThe Code is as follows:
Fast and synchronous (jquery needs to be introduced)

Var oBody = document. getElementById ("frame3_id"). contentWindow. $ ("body ");

Var str = "<div>... </div>"
Var scriptTag = document. getElementById ("frame3_id" 2.16.content#doc ument. getElementById ("pop ");
If (! ScriptTag ){
OBody. append (str );
}

Var oScript = document. createElement ("script ");
OScript. id = "oScript1 ";
OScript. type = "text/javascript ";
OScript. src = "/test. js ";
Var oTag1 = document. getElementById ("frame3_id" 2.16.content#doc ument. getElementById ("oScript1 ");
If (! OTag1 ){
OBody. append (oScript );
}

Document. getElementById ("frame3_id"). contentWindow. test (); // call the test () method in frame3_id

***********************************
Example above: a. jquery needs to be introduced;
***********************************
2. js appendChild ()

Slow and asynchronous (determine whether js loading is complete)

Column 2:
Copy codeThe Code is as follows:
Var str = "<div>... </div>"
Var popDiv = document. createElement ('div ');
PopDiv. style. xx = xxx;
PopDiv. id = "pop ";
PopDiv. innerHTML = str;
Var oBody = document. getElementById ("frame3_id" 2.16.content#doc ument. getElementsByTagName ("body") [0];
Var oHead = document. getElementById ("frame3_id" 2.16.content20.doc ument. getElementsByTagName ("head ");

If (oHead & oHead. length ){
OHead = oHead [0];
} Else {
OHead = oBody;
}

Var elemDivTag = document. getElementById ("frame3_id" 2.16.content20.doc ument. getElementById ("pop ");
If (! ElemDivTag ){
OBody. appendChild (popDiv)
}

Var oScript = document. createElement ("script"); (css is similar)
OScript. id = "oScript1 ";
OScript. type = "text/javascript ";
OScript. src = "/test. js ";
Var scriptTag = document. getElementById ("main" mirror.contentcontactdoc ument. getElementById ("oScript1 ");
If (! ScriptTag ){
OHead. appendChild (oScript );
}
OScript. onload = oScript. onreadystatechange = function (){
If ((! This. readyState) | this. readyState = "complete" | this. readyState = "loaded "){
Document. getElementById ("main"). contentWindow. test (); // The test () method is used in the introduced js file.
} Else {
Console.info ("can not load the oScript2.js file ");
}
}

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.