We help IT Professionals succeed at work.

append time in js file

716 Views
Last Modified: 2014-08-31
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
Comment
Watch Question

Most Valuable Expert 2011
Author of the Year 2014

Commented:
Do you have a server-side scripting language available, like PHP or ASP.net?
leakim971Multitechnician
CERTIFIED EXPERT
Distinguished Expert 2019

Commented:
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

Author

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....
leakim971Multitechnician
CERTIFIED EXPERT
Distinguished Expert 2019

Commented:
without server, it's not possible until you set this value somewhere and download it with ajax...

Author

Commented:
that what i need a common configurable program or entry in tomcat?
any sample code or link would be helpful
Most Valuable Expert 2011
Author of the Year 2014

Commented:
Let me try this one again:  Do you have a server-side scripting language available, like PHP or ASP.net?

Author

Commented:
It's servlet at server side
rrzstudent
CERTIFIED EXPERT

Commented:
<script src"./test.js?version=modificationtime">
Are these lines all in JSP files ?

Author

Commented:
yes
rrzstudent
CERTIFIED EXPERT

Commented:
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.
Michel PlungjanIT Expert
CERTIFIED EXPERT
Top Expert 2009

Commented:
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

rrzstudent
CERTIFIED EXPERT

Commented:
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.
rrzstudent
CERTIFIED EXPERT
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION

Author

Commented:
You could use the  WatchService to keep the application-scoped variables up to date.

Any sample code of above would be helpful
student
CERTIFIED EXPERT
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.