开发者

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'.

Straight from the jQuery Docs:

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. For example, if you want a text response to be treated as XML, use "text xml" for the dataType. You can also make a JSONP request, have it received as text, and interpreted by jQuery as 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

I am limited to making the dataType request of type 'scri开发者_如何学Gopt', or else I get a "...is not allowed by Access-Control-Allow-Origin" error. But by all rights, shouldn't I be free to interpret it within jQuery however I want to? I've clearly requested it as text, yet msg - the returned data from the server - is always 'undefined' regardless of what I do.

Are there any workarounds for this, hacky or not?

EDIT: This code works in that it loads the JavaScript file and downloads it to the user's browser. 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?

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.


Please note that I am using jQuery 1.6.1 with jQuery Mobile 1.0a4.1 and 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): }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜