Sometimes you need to dynamically introduce a piece of JavaScript after loading a DIV in the webpage. The solution under IE is as follows:
Reference content is as follows:
Newjs. onreadystatechange = function (){
If (newjs. readystate = 'loaded '){
// Ready!
}
}
Newjs. onload = function (){
// Ready!
}
Onload and onreadystatechange events are mainly used. This can also be used to dynamically load external CSS. Lower
It is suitable for both IE and Firefox.
Reference content is as follows:
VaR CSS;
Function include_css (css_file ){
VaR html_doc = Document. getelementsbytagname ('head') [0];
CSS = Document. createelement ('link ');
CSS. setattribute ('rel ', 'stylesheet ');
CSS. setattribute ('type', 'text/CSS ');
CSS. setattribute ('href ', css_file );
Html_doc. appendchild (CSS );
// Alert state change
CSS. onreadystatechange = function (){
If (CSS. readystate = 'complete '){
Alert ('css onreadystatechange fired ');
}
}
CSS. onload = function (){
Alert ('css onload fired ');
}
Return false;
}
VaR JS;
Function include_js (File ){
VaR html_doc = Document. getelementsbytagname ('head') [0];
JS = Document. createelement ('script ');
JS. setattribute ('type', 'text/JavaScript ');
JS. setattribute ('src', file );
Html_doc. appendchild (JS );
JS. onreadystatechange = function (){
If (JS. readystate = 'complete '){
Alert ('js onreadystate fired ');
}
}
JS. onload = function (){
Alert ('js onload fired ');
}
Return false;
}
From: http://www.chinaz.com/Design/Rules/09301G5R007.html