簡體   English   中英

如何在 JavaScript 中獲取 a.js 腳本的源代碼?

[英]How can I get the source code of a .js script within JavaScript?

$.ajax({url: 'http://gmaps-samples-v3.googlecode.com/svn-history/r16/trunk/xmlparsing/util.js', dataType: 'script text text', crossdomain:'true', success: function(msg, status, obj){console.log(msg);console.log(status);console.log(obj)}, mimetype: 'text/plain', cache:false});

我已經嘗試運行上面的代碼及其變體——刪除 mimetype、緩存、將 dataType 設置為“腳本文本”和“腳本腳本文本”。

直接來自 jQuery 文檔:

多個以空格分隔的值:從 jQuery 1.5 開始,jQuery 可以將 dataType 從它在 Content-Type header 中收到的內容轉換為您需要的內容。 例如,如果您希望將文本響應視為 XML,請使用“text xml”作為數據類型。 您還可以發出 JSONP 請求,將其作為文本接收,並由 jQuery 解釋為 XML:“jsonp text xml”。 類似地,諸如“jsonp xml”之類的速記字符串將首先嘗試從 jsonp 轉換為 xml,如果失敗,則從 jsonp 轉換為文本,然后從文本轉換為 xml

我僅限於發出“腳本”類型的 dataType 請求,否則我會收到“...訪問控制允許來源不允許”錯誤。 但是,無論如何,我不應該在 jQuery 中隨意解釋它嗎? 我已經明確要求它作為文本,但無論我做什么,msg - 從服務器返回的數據 - 總是“未定義”。

是否有任何解決方法,hacky與否?

編輯:此代碼的工作原理是它加載 JavaScript 文件並將其下載到用戶的瀏覽器。 但是我還是看不到!

但是,無論如何,我不應該在 jQuery 中隨意解釋它嗎?

瀏覽器中的安全機制會阻止您這樣做,因為它會允許您從其他網站竊取用戶的私人信息。 如果您向與腳本所在的域相同的域發出請求,它將起作用。 否則您無法在 JavaScript 中發出請求,而需要從服務器發出請求。

請注意,我將 jQuery 1.6.1 與 jQuery Mobile 1.0a4.1 和 PhoneGap 一起使用。

您的數據類型應該是“文本”而不是“腳本文本文本”:

$.ajax({url: 'http://gmaps-samples-v3.googlecode.com/svn-history/r16/trunk/xmlparsing/util.js', dataType: 'text', crossdomain:'true', success: function(msg, status, obj){console.log(msg);console.log(status);console.log(obj)}, mimetype: 'text/plain', cache:false});

該命令運行良好,並在日志中返回了以下內容:

D/PhoneGapLog(  240): file:///android_asset/www/js/myJSFile.js: Line 1 : /
**
D/PhoneGapLog(  240): * Returns an XMLHttp instance to use for asynchronous
D/PhoneGapLog(  240): * downloading. This method will never throw an exception,
but will
D/PhoneGapLog(  240): * return NULL if the browser does not support XmlHttp for
any reason.
D/PhoneGapLog(  240): * @return {XMLHttpRequest|Null}
D/PhoneGapLog(  240): */
D/PhoneGapLog(  240): function createXmlHttpRequest() {
D/PhoneGapLog(  240):  try {
D/PhoneGapLog(  240):    if (typeof ActiveXObject != 'undefined') {
D/PhoneGapLog(  240):      return new ActiveXObject('Microsoft.XMLHTTP');
D/PhoneGapLog(  240):    } else if (window["XMLHttpRequest"]) {
D/PhoneGapLog(  240):      return new XMLHttpRequest();
D/PhoneGapLog(  240):    }
D/PhoneGapLog(  240):  } catch (e) {
D/PhoneGapLog(  240):    changeStatus(e);
D/PhoneGapLog(  240):  }
D/PhoneGapLog(  240):  return null;
D/PhoneGapLog(  240): };
D/PhoneGapLog(  240):
D/PhoneGapLog(  240): /**
D/PhoneGapLog(  240): * This functions wraps XMLHttpRequest open/send function.
D/PhoneGapLog(  240): * It lets you specify a URL and will call the callback if
D/PhoneGapLog(  240): * it gets a status code of 200.
D/PhoneGapLog(  240): * @param {String} url The URL to retrieve
D/PhoneGapLog(  240): * @param {Function} callback The function to call once ret
rieved.
D/PhoneGapLog(  240): */
D/PhoneGapLog(  240): function downloadUrl(url, callback) {
D/PhoneGapLog(  240):  var status = -1;
D/PhoneGapLog(  240):  var request = createXmlHttpRequest();
D/PhoneGapLog(  240):  if (!request) {
D/PhoneGapLog(  240):    return false;
D/PhoneGapLog(  240):  }
D/PhoneGapLog(  240):
D/PhoneGapLog(  240):  request.onreadystatechange = function() {
D/PhoneGapLog(  240):    if (request.readyState == 4) {
D/PhoneGapLog(  240):      try {
D/PhoneGapLog(  240):        status = request.status;
D/PhoneGapLog(  240):      } catch (e) {
D/PhoneGapLog(  240):        // Usually indicates request timed out in FF.
D/PhoneGapLog(  240):      }
D/PhoneGapLog(  240):      if (status == 200) {
D/PhoneGapLog(  240):        callback(request.responseXML, request.status);
D/PhoneGapLog(  240):        request.onreadystatechange = function() {};
D/PhoneGapLog(  240):      }
D/PhoneGapLog(  240):    }
D/PhoneGapLog(  240):  }
D/PhoneGapLog(  240):  request.open('GET', url, true);
D/PhoneGapLog(  240):  try {
D/PhoneGapLog(  240):    request.send(null);
D/PhoneGapLog(  240):  } catch (e) {
D/PhoneGapLog(  240):    changeStatus(e);
D/PhoneGapLog(  240):  }
D/PhoneGapLog(  240): };
D/PhoneGapLog(  240):
D/PhoneGapLog(  240): /**
D/PhoneGapLog(  240):  * Parses the given XML string and returns the parsed docu
ment in a
D/PhoneGapLog(  240):  * DOM data structure. This function will return an empty
DOM node if
D/PhoneGapLog(  240):  * XML parsing is not supported in this browser.
D/PhoneGapLog(  240):  * @param {string} str XML string.
D/PhoneGapLog(  240):  * @return {Element|Document} DOM.
D/PhoneGapLog(  240):  */
D/PhoneGapLog(  240): function xmlParse(str) {
D/PhoneGapLog(  240):   if (typeof ActiveXObject != 'undefined' && typeof GetObj
ect != 'undefined') {
D/PhoneGapLog(  240):     var doc = new ActiveXObject('Microsoft.XMLDOM');
D/PhoneGapLog(  240):     doc.loadXML(str);
D/PhoneGapLog(  240):     return doc;
D/PhoneGapLog(  240):   }
D/PhoneGapLog(  240):
D/PhoneGapLog(  240):   if (typeof DOMParser != 'undefined') {
D/PhoneGapLog(  240):     return (new DOMParser()).parseFromString(str, 'text/xm
l');
D/PhoneGapLog(  240):   }
D/PhoneGapLog(  240):
D/PhoneGapLog(  240):   return createElement('div', null);
D/PhoneGapLog(  240): }
D/PhoneGapLog(  240):
D/PhoneGapLog(  240): /**
D/PhoneGapLog(  240):  * Appends a JavaScript file to the page.
D/PhoneGapLog(  240):  * @param {string} url
D/PhoneGapLog(  240):  */
D/PhoneGapLog(  240): function downloadScript(url) {
D/PhoneGapLog(  240):   var script = document.createElement('script');
D/PhoneGapLog(  240):   script.src = url;
D/PhoneGapLog(  240):   document.body.appendChild(script);
D/PhoneGapLog(  240): }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM