jquery Dynamic Loading Js/css file method (self-write small function) _jquery

Source: Internet
Author: User
Tags button type getscript

Let's take a look at jquery's own Getsrcript file.

Method

$.getscript (Url,callback)

Instance

Copy Code code as follows:

var TestVar = ' New JS loaded! ';
alert (TestVar); function Newfun (dynparam) {
Alert (' You just passed ' +dynparam+ ' as parameter. ');
}

Dynamic Invocation Method

Copy Code code as follows:

<script type= "Text/javascript" src= ". /jquery.js "></script>

<script type= "Text/javascript" >

$ (function ()

{

$ (' #loadButton '). Click (function () {

$.getscript (' New.js ', function () {

Newfun (' Checking new script ');//This function is inside the new.js, run this function when clicked click

});

});

});

</script>

<body>

<button type= "button" id= "Loadbutton" >Load</button>


The above can only dynamically load JS code, but can not load CSS, and later wrote a can load JS and CSS program.

The code is as follows

Copy Code code as follows:

$.extend ({
Includepath: ',
Include:function (file)
{
var files = typeof File = = "string"? [File]: file;
for (var i = 0; i < files.length; i++)
{
var name = Files[i].replace (/^s|s$/g, "");
var att = name.split ('. ');
var ext = att[att.length-1].tolowercase ();
var iscss = ext = "CSS";
var tag = iscss? "Link": "Script";
var attr = iscss? "Type= ' text/css ' rel= ' stylesheet '": "language= ' JavaScript ' type= ' Text/javascript '";
var link = (iscss? "href": "src") + "= '" + $.includepath + name + "'";
if ($ (tag + "[" + link + "]"). Length = = 0 document.write ("<" + tag + attr + link + "></" + tag + ">");
}
}
});
$.include ([' Hdivbox.js ', ' pop_win.css ']);

Related Article

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.