append time in js file

hi,

how to append the file modification date & time in the javascript file?
test.js is modified on 12-july-2008 then it should append this date(format no bar)
again test.js is modified on 12-aug-2009 it should append this date.


<script src"./test.js?version=modificationtime">

something like below in jsp

http://www.codeproject.com/Articles/203288/Automatic-JS-CSS-versioning-to-update-browser-cach
RockingAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Ray PaseurCommented:
Do you have a server-side scripting language available, like PHP or ASP.net?
0
leakim971PluritechnicianCommented:
Replace you line with the following :
<script>
var modificationtime =  new Date().getTime();
var script = document.createElement("script");
script.async = false;
script.charset = "utf8",
script.src = "./test.js?version=" + modificationtime;
var head = document.head || document.getElementsByTagName("head")[0] || document.documentElement;
head.insertBefore( script, head.firstChild );
</script>

Open in new window

0
RockingAuthor Commented:
var modificationtime =  new Date().getTime();

this code will verytime return current date and time,what i need is the date and time should only change when file is modified.
i need file modification date and time....
0
The Ultimate Tool Kit for Technolgy Solution Provi

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy for valuable how-to assets including sample agreements, checklists, flowcharts, and more!

leakim971PluritechnicianCommented:
without server, it's not possible until you set this value somewhere and download it with ajax...
0
RockingAuthor Commented:
that what i need a common configurable program or entry in tomcat?
any sample code or link would be helpful
0
Ray PaseurCommented:
Let me try this one again:  Do you have a server-side scripting language available, like PHP or ASP.net?
0
RockingAuthor Commented:
It's servlet at server side
0
rrzCommented:
<script src"./test.js?version=modificationtime">
Are these lines all in JSP files ?
0
RockingAuthor Commented:
yes
0
rrzCommented:
How many web pages do you have in your web app?
 Could you rewrite each URL in the src attribute for each script tag?  I am suggesting something like
 <script src"./test.js?version=<m:modTime file="test.js" />">
where I have substituted  modificationtime with a custom tag.  The custom tag could get the last modified time for the file. It is easy to create a custom file using "tag files". I could post some demonstration code tomorrow. Just tell us about your file structure.
Is test.js in the same folder as your JSP?
Would  
<script src"test.js?version=modificationtime">  
work just as well? Notice the change in the URL.
0
Michel PlungjanIT ExpertCommented:
Here is how to do this on the client side using jQuery ajax

Live Demo

function getLast(fileName) {
    $.ajax({
        type: "HEAD",
        async: true,
        url: fileName,
        success: function(message,text,response){
           var lm = response.getResponseHeader('Last-modified');
           if (lm) {
             $("#res").text(lm +"("+new Date(lm).getTime()+")");
           }
        }

    });
}

Open in new window

0
rrzCommented:
Here are a couple more ideas.
Does your Tomcat use Java 1.7 or 1.8 ? If so, there is WatchService  
http://docs.oracle.com/javase/7/docs/api/java/nio/file/WatchService.html   
 that might come in handy.
 Could you rewrite each URL in the src attribute for each script tag?  I am suggesting something like
 <script src"./test.js?version=${applicationScope.testmodificationtime}">
 You could use the  WatchService to keep the application-scoped variables up to date. The variables could be set in a ServletContextListener.

Alternatively, if WatchService is too complicated, then use A TimerTask to periodically check for file changes. It could keep the  application- scoped variables up to date.
0
rrzCommented:
Here is the custom tag solution. If your web app gets a lot of traffic and the script files don't change that often, then maybe my other ideas would be better. Anyway, here is the  JSP.
<%@ taglib tagdir="/WEB-INF/tags" prefix="m" %>
testing
 <script src"test.js?version=<m:modTime file="test.js" />">

Open in new window

 In your web app's WEB-INF  folder, create a folder named tags.  Create a file called  modTime.tag  with the following  content and put it into the tags folder.
<%@ tag import="java.io.*" %><%@ attribute name="file" required="true" %><%
File f = new File(application.getRealPath(file));
out.print(f.lastModified());%>

Open in new window

Don't change the formatting. If you use standard formatting in the tag file, then the  source code in the browser won't look pretty. You could change the names I have used(m and modTime) to suit yourself.
0
RockingAuthor Commented:
You could use the  WatchService to keep the application-scoped variables up to date.

Any sample code of above would be helpful
0
rrzCommented:
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Apache Web Server

From novice to tech pro — start learning today.