One include function implemented by JS many languages have functions similar to the include and require functions in php, but javascript does not.
I tried to implement similar functions in js for a long time and tried many times but failed. When I searched google for keywords about dynamic loading of css files, I found a function on a blog (the address cannot be found) that adds the link element to the head tag of the webpage. So I changed it and wrote this function.
Usage:
Include_js (src, [reload]);
Src: Path Name of the js File
Reload: optional parameter, 0 or 1, indicating whether to repeatedly load the js file of the same url.
Note:
When the contained js file contains document. the write method does not respond in IE, but in Mozilla Firefox, your original webpage disappears and only displays document. write content: mad:
Hey, I naturally have some tips for this problem:
The document. write method is redefined so that it does not display anything. Specific Practices:
Add this sentence before include_js.
Document. write = function () {return false ;}
In this way, even if the include Javascript file contains the document. write method, we are not afraid that it will destroy the webpage! : Lol:
Purpose:
This function can be used for asynchronous loading of js files for some advertisements and statistics, avoiding slow webpage display caused by loading js files.
You can modify this function to dynamically load css files, but it is useless to load JavaScript files.
The Code is as follows:
Function compute de_js (path, reload)
{
Var scripts = document. getElementsByTagName ("script ");
If (! Reload)
For (var I = 0; I If (scripts [I]. src & scripts [I]. src. toLowerCase () = path. toLowerCase () return;
Var sobj = document. createElement ('script ');
Sobj. type = "text/javascript ";
Sobj. src = path;
Var headobj = document. getElementsByTagName ('head') [0];
Headobj. appendChild (sobj );
}