I. Dynamic creation of link methods
We can use link in the same way as shown in the following code.
[JavaScript]View Plaincopyprint?
- function Addcssbylink (URL) {
- var doc=document;
- var link=doc.createelement ("link");
- Link.setattribute ("rel", "stylesheet");
- Link.setattribute ("type", "text/css");
- Link.setattribute ("href", url);
- var heads = doc.getelementsbytagname ("Head");
- if (heads.length)
- Heads[0].appendchild (link);
- Else
- Doc.documentElement.appendChild (link);
- }
- var style=doc.createelement ("style");
- Style.setattribute ("type", "text/css");
- if (style.stylesheet) {//IE
- Style.styleSheet.cssText = cssstring;
- } else {//
- var csstext = Doc.createtextnode (cssstring);
- Style.appendchild (Csstext);
- }
- var heads = doc.getelementsbytagname ("Head");
- if (heads.length)
- Heads[0].appendchild (style);
- Else
- Doc.documentElement.appendChild (style);
- }
In this case, if it is less code, it can be more convenient to implement the effect of dynamic loading CSS, but if in order to facilitate maintenance and management, and do not wait for the time limit, the use of link method more appropriate
"Script");
- Script.setattribute ("type", "Text/javascript");
- Script.setattribute ("src", "justwalking.js");
- var heads = document.getElementsByTagName ("Head");
- if (heads.length)
- Heads[0].appendchild (script);
- Else
- Document.documentElement.appendChild (script);
But this way in the IE kernel browser support, Google, 360 speed, Firefox but not
Four. Printing introduces style mode
[JavaScript]View Plaincopyprint?
- document.write ("<link rel=\" stylesheet\ "href=\" uild/style.css\ "type=\" text/css\ "media=\" screen\ "/>");
Five. Printing introduces JS mode
[JavaScript]View Plaincopyprint?
- document.write ("<script type=\" text/javascript\ "src=\" justwalking.js\ "></script>");