繁体   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