Asynchronously loading and executing a JS file using the Getscript () method

Source: Internet
Author: User

Asynchronously loading and executing a JS file using the Getscript () method

Use the getScript() method to asynchronously request and execute a file in JavaScript format on the server, whose invocation format is as follows:

jQuery.getScript(url,[callback])Or$.getScript(url,[callback])

The parameter URL is the server request address, and the optional callback parameter is the callback function that executes after the request succeeds.

1<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">234<title> asynchronously loads and executes the JS file using the Getscript () method </title>5<script src= "Http://libs.baidu.com/jquery/1.9.0/jquery.js" type= "Text/javascript" ></script>6<style>7 #divtest8             {9 width:282px;Ten             } One #divtest. Title A             { - padding:8px; -background-Color:blue; the color: #fff; - height:23px; -line-height:23px; -font-size:15px; +font-Weight:bold; -             } + ul A             { at                 float: Left; - width:280px; - padding:5px 0px; - margin:0px; -font-size:14px; -list-style-Type:none; in             } - ul Li to             { +                 float: Left; - width:280px; the height:23px; *line-height:23px; $ padding:3px 8px;Panax Notoginseng             } - . FL the             { +                 float: Left; A             } the . Fr +             { -                 float: Right; $             } $</style> - -      the<body> -<div id= "Divtest" >Wuyi<div class= "title" > the<span class= "FL" > My favorite Sport </span> -<span class= "FR" > Wu<input id= "btnshow" type= "button" value= "Load"/> -</span> About</div> $<ul></ul> -</div> -          -<script type= "Text/javascript" > A$(function () { +$ ("#btnShow"). Bind ("click",function () { the                     var$ This= $( This); -$.getscript ("./sport.js",function(){ $$ This. attr ("Disabled", "true"); the                     }); the                 }) the             }); the</script> -</body> inView Code
1 vardata = [{2"Name": "Football"3     },{4"Name": "Basketball"5     },{6"Name": "Table tennis"7     },{8"Name": "Volleyball"9 }];Ten$.each (data,function(index,sport) { One$ ("ul"). Append ("<li>" +sport[' name ']+ "</li>"); A});
View Code

Asynchronously loading and executing a JS file using the Getscript () method

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.