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>