1

I have a question! I'm not sure if this is possible, but...

I have on a page a javascript include file. At the top of the file is a comment, looks something along the lines of this:

/*
  Local site config.js 
  Last updated: 2012.01.04
*/

What I would like to be able to do is within another javascript file (this file gets loaded after the first file), somehow get that date. So my question is, are comments somehow stored within the DOM, or is there some other way to access the contents of this file in a way that I could just use regex to scrape for it, etc..?

Basically I have a global file that depends on local files for some config options, and there are a ton of sites that have their own local config file. I would like to be able to grab that date from the local .js file within the global file. Both local and global .js files are hosted on the same domain, but the sites that include the .js files may or may not be on the same domain as the .js files.

I know the right answer is to update all the local .js files to put that date in a .js variable, and as a long term solution, I certainly intend on doing that. But as a short term solution the only thing I can change right now is the global .js file. If it can't be done then it can't be done, but I wanted to explore my options and so far I can't think of any way to get that date...Soo... anybody have any ideas?

slinkhi
  • 947
  • 4
  • 16
  • 32
  • Comments are not kept after being parsed so you would need to open the file like you would a text file and then read in the comment manually. Then you could parse this as a date variable in your file – jzworkman Mar 02 '12 at 15:36
  • @T.J.Crowder well yea but they are not available to other files the way he wants to access them, I just worded that poorly. I edited it to reflect that. – jzworkman Mar 02 '12 at 15:37

3 Answers3

3

So my question is, are comments somehow stored within the DOM...

Nope.

...or is there some other way to access the contents of this file in a way that I could just use regex to scrape for it, etc..?

Not without loading it a second time via ajax, and that would be limited by the SOP. E.g, you can do an ajax GET for the file, which will give you its text, and then apply a regular expression to find the bit after "Last updated:". But the .js file would have to be on the same origin as the HTML document you're doing this in (see the link for why).

Ideally, as you said, you want to modify the first file so that it saves that date in a variable where you can access it. (This would probably end up being a global variable, which is less than ideal, unless you already have some other global you can stick it on as a property.)

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
  • 1
    The local .js files are pretty short, so as a short term solution I don't mind having to load it a 2nd time, but as you pointed out, I knew I wouldn't be able to use ajax for it anyways. I figured there wasn't some other magic wand I was missing but no harm asking. Thanks! – slinkhi Mar 02 '12 at 15:56
  • @mysql_noobie_xxxx: As you say, no harm in asking! Good luck, – T.J. Crowder Mar 02 '12 at 16:01
1

The comments are not in the DOM. You have to read the script file.

See: How can I get the content of the file specified as the 'src' of a <script> tag?

Community
  • 1
  • 1
Diodeus - James MacFarlane
  • 112,730
  • 33
  • 157
  • 176
0

You might be able to use reflection to do it. This link explains a rather involved way of getting code comments out of the body of a function using a regular expression, so maybe it's possible to do this with whole file too, but it's probably a lot more hassle than using a local variable.

I'd recommend using PHP or something to loop over the files in question using a regular expression to extract the data and writing it back to the file as a local variable. Probably less of a headache.

Matt Gibson
  • 14,616
  • 7
  • 47
  • 79
  • 1
    *"You might be able to use reflection to do it."* Not for what the OP described, no; and not even reliably on individual functions. First off, the technique only works with the source of specific functions, because it uses `Function#toString`. Second, `Function#toString` has never been standardized and it's perfectly acceptable for an implementation to return anything it likes, including "code here" (though all major *desktop* browsers do return source). Third, not all browsers that do give source out of `toString` include comments with it. – T.J. Crowder Mar 02 '12 at 15:51