Javascript-implemented include function _ javascript skills

Source: Internet
Author: User
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 );
}

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.