Dynamic jquery Library Loading Method

Source: Internet
Author: User

Sometimes, we may not <script src = "jquery. min. js" in the webpage to load the Jquery library. Users may only load the Jquery library after clicking a button. I don't need to talk about the advantages of saving bandwidth and improving access speed, because users may not click this button, so they do not need Jquery. So how to dynamically load the Jquery library? Generally, you can print it out using Document. write, Ajax, or the following method:

Copy codeThe Code is as follows:
<! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<Html xmlns = "http://www.w3.org/1999/xhtml">
<Head>
<Title> dynamically load the Jquery library without creating Ajax requests. Write one or more pieces of code in the href attribute of a </title>
<Style type = "text/css">
# Message {margin: 20px 10px; color: Green ;}
</Style>
<Script language = "javascript" type = "text/javascript">
Function AjaxLoadJquerylibrary ()
{
Var d = document, s = d. getElementById ('firebug-lite ');
If (s! = Null)
Return;
S = d. createElement ('script ');
S. type = 'text/javascript ';
S. src = 'HTTP: // ajax.googleapis.com/ajax/libs/jquery/1.3.1/jquery.min.js ';
D. body. appendChild (s );
Document. getElementById ("content"). style. display = "block ";
}
</Script>
</Head>
<Body>
<Div>
<Input type = "button" value = "dynamically import Jquery" onclick = "AjaxLoadJquerylibrary ();"/> <br/>
</Div>
<Div id = "message">

</Div>
<Div id = "content">
Enter your name: <input type = "text" value = "" id = "txtUserName"/> <br/>
<A href = "javascript :( function () {var username = $ ('# txtusername '). val (); alert (username) ;}) (); void (0); "> Jquery gets the name </a>
</Div>
</Body>
</Html>

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.