Question

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?

Was it helpful?

Solution

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

OTHER TIPS

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?

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top