Solved

Reload multiple images with variable filenames using javascript

Posted on 2007-03-23
4
367 Views
Last Modified: 2013-12-25
I have a perl cgi script that generates reports that may contain 1 to 100+ images. How can I reload the images (each with a filename generated using a datetime stamp) using javascript?

I'm a javascript newbie and I don't know how to deal with multiple images with variable filenames. How do I pass the filenames generated by the perl cgi script to a javascript function?

Thanks.
0
Comment
Question by:bespnmom
  • 3
4 Comments
 
LVL 63

Expert Comment

by:Zvonko
ID: 18783536
If the majority of the images on the page has to be refreshed then I would recommand to refresh ALL images on the page.
Like this:

<script>
function reFresh(){
  var img = document.images;
  for(var i=0;i<img.length;i++){
    img.src = img.src.replace(/\?t\=\d+/g,"")+"?t="+(new Date()).getTime();
  }
}
</script>


0
 

Author Comment

by:bespnmom
ID: 18806725
I can't get it to work. What does this:
            img.src.replace(/\?t\=\d+/g,"")+"?t="+(new Date()).getTime();
do?
0
 
LVL 63

Expert Comment

by:Zvonko
ID: 18807027
That statement generates the new src file name or url for the image so that it can be refreshed by new name.
That is done in upper statement in two steps:
1.) removing of previous time parameter from the image src String.
2.) adding of new time parameter to the src string.

0
 
LVL 63

Accepted Solution

by:
Zvonko earned 500 total points
ID: 18807041
Sorry, I missed the loop variable in the img Array.
Check better this:

<script>
function reFresh(){
  var img = document.images;
  for(var i=0;i<img.length;i++){
    img[i].src = img[i].src.replace(/\?t\=\d+/g,"")+"?t="+(new Date()).getTime();
  }
}
reFresh()
</script>


0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now