Asp. Net + Jquery. Ajax details 5-$. getScript

Source: Internet
Author: User
Tags getscript

Directory (updated articles will be connected, and an article will be updated every two to three days starting from March 13, July 25 ):


Asp. Net + Jquery. Ajax details 1-opening section (published on February 25)

Asp. Net + Jquery. Ajax details 2-$. Load (published on 2012.07.26)

Asp. Net + Jquery. Ajax details 3-$. get and $. post (published on February 30)

Asp. Net + Jquery. Ajax details 4-$. getJSON (published on February 31)

Asp. Net + Jquery. Ajax details 5-$. getScript (2012.08.04)

Asp. Net + Jquery. Ajax details 6-$. ajaxSetup

Asp. Net + Jquery. Ajax details 7-Global Ajax events

Asp. Net + Jquery. Ajax details 8-core $. ajax

Asp. Net + Jquery. Ajax details 9-serialize and serializeArray

Asp. Net + Jquery. Ajax detailed description: 10-JSON and XML + written at the end

 

 


JQuery. getScript (url, [callback])

Load and execute a JavaScript file through an http get request.

 

Url: the url of the JS file to be loaded.

Callback: callback function after successful loading.

The jQuery internal implementation of this function still uses the get function. getScript imports the type parameter with the value of "script". Finally, the Ajax function processes the request with the type as script as follows:

 

[Javascript]
Var head = document. getElementsByTagName ("head") [0];
 
Var script = document. createElement ("script ");
 
Script. src = s. url;

Var head = document. getElementsByTagName ("head") [0];

Var script = document. createElement ("script ");

Script. src = s. url;
 

The above js Code creates a script statement block and adds it to the head:

Head. appendChild (script );

After the script is loaded, the js Code deleted from the header will be omitted. If you are interested, study Jquery.

I only did a non-Cross-origin test, and I will have time to complete another cross-origin test.

 

Instance:

Client --

[Javascript]
<% @ Page Language = "C #" AutoEventWireup = "true" CodeBehind = "JqueryAjaxGetScript. aspx. cs" Inherits = "JqueryAjaxTest. JqueryAjaxGetScript" %>
 
<! 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 runat = "server">
<Title> jquery ajax test </title>
<Script src = "Scripts/jquery-1.7.2.min.js" type = "text/javascript"> </script>
<Script type = "text/javascript">

$ (Function (){
// Bind an event to the button
$ ("# TestGetScript"). bind ("click", GetScriptTest );
$ ("# Button1"). bind ("click", GetScriptTest );

});
 
 
// Test getScript
Function GetScriptTest (event ){
 
$. GetScript ("Scripts/test. js", function (responseText, textStatus ){

$ ("# Result" ).html ("the content of the requested js file is:" + responseText + "<br/>" + "Request status: "+ textStatus +" <br/> "+" request js url: "+ this. url );
});
}

</Script>
</Head>
<Body>
<Form id = "form1" runat = "server">
<Div>
<Input id = "TestGetScript" type = "button" value = "test jquery. getScript"/>
<Div id = "result">
</Div>

</Div>
</Form>
</Body>
</Html>

<% @ Page Language = "C #" AutoEventWireup = "true" CodeBehind = "JqueryAjaxGetScript. aspx. cs" Inherits = "JqueryAjaxTest. JqueryAjaxGetScript" %>

<! 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 runat = "server">
<Title> jquery ajax test </title>
<Script src = "Scripts/jquery-1.7.2.min.js" type = "text/javascript"> </script>
<Script type = "text/javascript">

$ (Function (){
// Bind an event to the button
$ ("# TestGetScript"). bind ("click", GetScriptTest );
$ ("# Button1"). bind ("click", GetScriptTest );

});


// Test getScript
Function GetScriptTest (event ){

$. GetScript ("Scripts/test. js", function (responseText, textStatus ){

$ ("# Result" ).html ("the content of the requested js file is:" + responseText + "<br/>" + "Request status: "+ textStatus +" <br/> "+" request js url: "+ this. url );
});
}
 
</Script>
</Head>
<Body>
<Form id = "form1" runat = "server">
<Div>
<Input id = "TestGetScript" type = "button" value = "test jquery. getScript"/>
<Div id = "result">
</Div>

</Div>
</Form>
</Body>
</Html>

The javascript code in test. js requested by the client is as follows:

[Javascript] www.2cto.com
Alert ("the javascript file test. js of the getScript request has been loaded! ");

Alert ("the javascript file test. js of the getScript request has been loaded! ");

The test code is simple and the results are clear at a glance.

 


Author: shan9liang

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.