JQuery AJAX call - how to save comments

context:. In the version for developing a JavaScript project, I wrote a small utility for tracking script dependencies (because I don’t like the fun syntax requirements of existing libraries). I wanted it to work in the same way as other languages, so I decided to parse the imported files for comments as follows:

// DEPENDS: myurl.js // 

Build and evaluate dependency graphs and select dependencies using jQuery $ .ajax (). This works well with top level files. However, files that remain unanswered using an ajax call are deprived of comments, so I cannot analyze the dependencies again.

For your interest: Cycle

  • fetch file
  • parse regular expression content for dependencies
  • expand the dependency graph, if necessary
  • start with (1) if (3) happened (with the necessary files, of course)
  • evaluate script files in order

Q: Is there a way to stop jQuery from removing JavaScript comment tags from files uploaded by $ .ajax ()?

code: I call ajax this way

 $.ajax({ url: fileName, dataType: 'text', context: this, success: function(jqXHR) { this.parseImport(fileName, jqXHR); } }); 

(from within the method providing fileName and that is correct). I was hoping that dataType: "text" would force jQuery to import the text literally, but, well, it is not: -D

The API document for $ .ajax () states that the contents of dataType: text elements are processed using window.String, but I'm not sure what window.String does to input it. Maybe the solution is somewhere in this part?

I would be happy if someone could point me in the right direction.

+7
jquery ajax
source share
2 answers

The way you use ajax will not remove comments from the file. You are requesting jQuery to download text from the server. He will download this text, right, without any changes. I suspect you have a minimization / compression process on the server that removes comments. This jQuery code will not.

But you can still get around everything you need to remove comments - without using comments. Instead, you can use the line:

 "DEPENDS: myurl.js"; 

Since this is not a comment, it should not be shared. It looks a little strange at first, but the JavaScript syntax has an ExpressionStatement expression, which is an expression consisting solely of an expression. And, of course, the string literal itself is a valid expression. (So ​​they did "use strict"; in ES5, without causing problems for JavaScript engines before ES5.)

+4
source share

A solution with a string is a good one, but I really think there is another good solution.

Just define your ajax mechanism, for example. "myscript" with ajaxTransport . For example. see this link : you can register your method, which will return everything as you like; In addition, you can analyze comments directly in the transport and return dependencies as an array.

This will encapsulate your parsing methods and build them in an ajax call, as if it were your own standard JavaScript file.

To learn more (they actually have no examples), see jQuery docs

0
source share

All Articles