简体   繁体   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});

I have tried running the above code and variants of it - removing mimetype, cache, setting dataType to 'script text' and 'script script text'.我已经尝试运行上面的代码及其变体——删除 mimetype、缓存、将 dataType 设置为“脚本文本”和“脚本脚本文本”。

Straight from the jQuery Docs:直接来自 jQuery 文档:

multiple, space-separated values: As of jQuery 1.5, jQuery can convert a dataType from what it received in the Content-Type header to what you require.多个以空格分隔的值:从 jQuery 1.5 开始,jQuery 可以将 dataType 从它在 Content-Type header 中收到的内容转换为您需要的内容。 For example, if you want a text response to be treated as XML, use "text xml" for the dataType.例如,如果您希望将文本响应视为 XML,请使用“text xml”作为数据类型。 You can also make a JSONP request, have it received as text, and interpreted by jQuery as XML: "jsonp text xml."您还可以发出 JSONP 请求,将其作为文本接收,并由 jQuery 解释为 XML:“jsonp text xml”。 Similarly, a shorthand string such as "jsonp xml" will first attempt to convert from jsonp to xml, and, failing that, convert from jsonp to text, and then from text to xml类似地,诸如“jsonp xml”之类的速记字符串将首先尝试从 jsonp 转换为 xml,如果失败,则从 jsonp 转换为文本,然后从文本转换为 xml

I am limited to making the dataType request of type 'script', or else I get a "...is not allowed by Access-Control-Allow-Origin" error.我仅限于发出“脚本”类型的 dataType 请求,否则我会收到“...访问控制允许来源不允许”错误。 But by all rights, shouldn't I be free to interpret it within jQuery however I want to?但是,无论如何,我不应该在 jQuery 中随意解释它吗? I've clearly requested it as text, yet msg - the returned data from the server - is always 'undefined' regardless of what I do.我已经明确要求它作为文本,但无论我做什么,msg - 从服务器返回的数据 - 总是“未定义”。

Are there any workarounds for this, hacky or not?是否有任何解决方法,hacky与否?

EDIT: This code works in that it loads the JavaScript file and downloads it to the user's browser.编辑:此代码的工作原理是它加载 JavaScript 文件并将其下载到用户的浏览器。 But I still can't view it!但是我还是看不到!

But by all rights, shouldn't I be free to interpret it within jQuery however I want to?但是,无论如何,我不应该在 jQuery 中随意解释它吗?

Security mechanisms in browsers prevent you from doing this, because it would allow you to steal users' private information from other websites.浏览器中的安全机制会阻止您这样做,因为它会允许您从其他网站窃取用户的私人信息。 If you are making the request to the same domain as your script is on, it will work.如果您向与脚本所在的域相同的域发出请求,它将起作用。 Otherwise you cannot make the request in JavaScript, and need to make it from a server instead.否则您无法在 JavaScript 中发出请求,而需要从服务器发出请求。

Please note that I am using jQuery 1.6.1 with jQuery Mobile 1.0a4.1 and PhoneGap.请注意,我将 jQuery 1.6.1 与 jQuery Mobile 1.0a4.1 和 PhoneGap 一起使用。

Your dataType should be "text" instead of "script text text":您的数据类型应该是“文本”而不是“脚本文本文本”:

$.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});

This command worked fine and returned me the following in the logs:该命令运行良好,并在日志中返回了以下内容:

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