Asp.Net+Jquery.Ajax详解5-$.getScript
?
目錄(已經更新的文章會有連接,從7月25日開始,每2到3天更新一篇):
Asp.Net+Jquery.Ajax詳解1-開篇(2012.07.25發)
Asp.Net+Jquery.Ajax詳解2-$.Load(2012.07.26發)
Asp.Net+Jquery.Ajax詳解3-$.get和$.post(2012.07.30發)
Asp.Net+Jquery.Ajax詳解4-$.getJSON(2012.07.31發)
Asp.Net+Jquery.Ajax詳解5-$.getScript(2012.08.04發)
Asp.Net+Jquery.Ajax詳解6-$.ajaxSetup(2012.08.06發)
Asp.Net+Jquery.Ajax詳解7-全局Ajax事件(2012.08.09發)
Asp.Net+Jquery.Ajax詳解8-核心$.ajax(2012.08.12發)
Asp.Net+Jquery.Ajax詳解9-serialize和serializeArray(2012.08.15發)
Asp.Net+Jquery.Ajax詳解10-JSON和XML+寫在最后(2012.08.20發,結束啦!)
?
?
jQuery.getScript(url, [callback])
通過 HTTP GET 請求載入并執行一個 JavaScript 文件。
?
url:待載入 JS 文件地址。
callback:成功載入后回調函數。
此函數的jQuery內部實現, 仍然使用get函數, getScript將傳入值為"script"的type參數, 最后在Ajax函數中對type為script的請求做了如下處理:
?
var head = document.getElementsByTagName("head")[0]; var script = document.createElement("script");script.src = s.url;?
通過以上js代碼建立了一個script語句塊, 并加入到head中:
head.appendChild(script);
當腳本加載完畢后, 再從head中刪除.刪除的js代碼就省略了,有興趣自己去研究Jquery.
我僅僅做了一個非跨域的測試,以后有時間再補一個跨域的.
?
實例:
客戶端——
<%@ 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 () {//為按鈕綁定事件$("#TestGetScript").bind("click", GetScriptTest);$("#Button1").bind("click", GetScriptTest);});//測試getScriptfunction GetScriptTest(event) {$.getScript("Scripts/test.js", function (responseText, textStatus) {$("#result").html("請求的js文件的內容為:" + responseText + "<br/>" + "請求狀態:" + textStatus + "<br/>" + "請求js的url:" + this.url);});}</script> </head> <body><form id="form1" runat="server"><div><input id="TestGetScript" type="button" value="測試jquery.getScript" /><div id="result"></div></div></form> </body> </html>
客戶端請求的test.js中的javascript代碼如下:
?
測試代碼很簡單,獲得的效果一目了然,不再贅述了。
轉載于:https://www.cnblogs.com/javaspring/archive/2012/08/04/2656490.html
總結
以上是生活随笔為你收集整理的Asp.Net+Jquery.Ajax详解5-$.getScript的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【啊哈!算法】之二、插入排序
- 下一篇: 设计模式建议学习顺序