Originally posted by NickFitz
View Post
- Visitors can check out the Forum FAQ by clicking this link. You have to register before you can post: click the REGISTER link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below. View our Forum Privacy Policy.
- Want to receive the latest contracting news and advice straight to your inbox? Sign up to the ContractorUK newsletter here. Every sign up will also be entered into a draw to WIN £100 Amazon vouchers!
Reply to: Joomla Javascript Problem
Collapse
You are not logged in or you do not have permission to access this page. This could be due to one of several reasons:
- You are not logged in. If you are already registered, fill in the form below to log in, or follow the "Sign Up" link to register a new account.
- You may not have sufficient privileges to access this page. Are you trying to edit someone else's post, access administrative features or some other privileged system?
- If you are trying to post, the administrator may have disabled your account, or it may be awaiting activation.
Logging in...
Previously on "Joomla Javascript Problem"
Collapse
-
Looks interesting but I don't have a clue how to use it in Joomla. It would be much simpler if I had code that would work within each of the two modules.
-
Back on topic: the following code will auto-refresh any images in the page that have a name attribute whose value is "refresh" followed by zero or more digits, such as "refresh", "refresh1", "refresh99999".
Notes:- All images to be refreshed must have a different name.
- All images lacking a name, or with a name not matching the pattern, will be left alone.
- If no images are found with a suitable name, the refresh timer won't be started.
- The code creates no global variables, and will neither interfere with nor be interfered with by any other code on the page, unless that code is also trying to change the src attribute of the same images.
- The code only needs to be included once, and must be after all the images. The best place is immediately before the </body> tag.
- It automatically picks up the original path/name of the image from the HTML, so even if you change the source of one of the images it will still work.
- You can change the refresh interval at the line near the top with a comment about it.
- If you want it on multiple pages, it's best to put it in an external file and use <script src="image-refresh.js"></script>.
- I've put up a demo page which refreshes two separate images every two seconds whilst leaving another untouched; the images just show the date and time that the browser asked for them.
- It's been tested on Safari 5 Mac, Firefox 3.6 Mac, and IE6 XP, but it should be OK anywhere - in fact, it should even work on Netscape Navigator 3

Code:(function() { // Demo: http://www.nickfitz.co.uk/examples/javascript/image-refresh/image-refresh.html var config = { refreshInterval: 10, // This is the refresh interval in seconds imageMatcher: /^refresh\d*$/ }, refreshImages = [], images, refreshImagesLength; images = document.getElementsByTagName("img"); for (var i = 0, l = images.length; i < l; i++) { if (images[i].name.match(config.imageMatcher)) { refreshImages.push({img: images[i], src: images[i].src}); } } refreshImagesLength = refreshImages.length; if (refreshImagesLength) { setInterval(function() { for (var i = 0; i < refreshImagesLength; i++) { refreshImages[i].img.src = refreshImages[i].src + "?" + new Date().getTime(); } }, config.refreshInterval * 1000); } })();Last edited by NickFitz; 4 September 2010, 21:11.
Leave a comment:
- All images to be refreshed must have a different name.
-
Almost rightOriginally posted by d000hg View PostIt's optional in JS
JS will perform automatic semicolon insertion according to a precisely-specified algorithm, but if you don't know that algorithm, you can get caught out: for example, the following code will produce a syntax error because the single line without a semicolon (highlighted for your viewing pleasure) won't have a semicolon inserted automatically according to the rules:
Best practise is to never rely on automatic semicolon insertion, and furthermore to understand how it works so you don't get caught out by it when it does happen, such as:Code:var bar = "a"; var baz = {}; var boo = 1; var bop = 2; a = boo + bop (baz).foo = bar;
where a semicolon will be inserted after "return", meaning that the function will return "undefined" rather than returning that nice object defined by the object literal we started on the next line to keep it nicely formatted.Code:function foo() { // stuff happens... return { a: "foo", b: 7 }; }
Leave a comment:
-
The exact code I'm using I posted above the only difference being the code is duplicated in two separate Joomla modules.Originally posted by Jaws View PostDifficult to know exactly without seeing your current code but perhaps changing it to:
<div align="center"><img src="mypath/cam_1.jpg" name="refresh" height="240" width="320" />
<script type="text/javascript">
var t = 10; // interval in seconds
image1 = "mypath/cam_1.jpg"; //name of the image
image2 = "mypath/cam_2.jpg";
function Start() {
tmp = new Date();
tmp = "?"+tmp.getTime()
document.images["refresh1"].src = image1+tmp;
document.images["refresh2"].src = image2+tmp;
setTimeout("Start()", t*1000)
}
Start();
</script>
</div>
Leave a comment:
-
Difficult to know exactly without seeing your current code but perhaps changing it to:
<div align="center"><img src="mypath/cam_1.jpg" name="refresh" height="240" width="320" />
<script type="text/javascript">
var t = 10; // interval in seconds
image1 = "mypath/cam_1.jpg"; //name of the image
image2 = "mypath/cam_2.jpg";
function Start() {
tmp = new Date();
tmp = "?"+tmp.getTime()
document.images["refresh1"].src = image1+tmp;
document.images["refresh2"].src = image2+tmp;
setTimeout("Start()", t*1000)
}
Start();
</script>
</div>
Leave a comment:
-
Can you add proper semicolons to your code, this may or may not have effect, but let's just be proper and end all statements with ;
Also make correct path image = "cam/cam_2.jpg" - should be: image = /"cam/cam_2.jpg";
Also add to IMG tags IDs, ie: ID="refresh1" and ID="refresh2", not sure it would help but give it a go - it should have worked by now with the changes you made.
Leave a comment:
-
I renamed cam 1 to;Originally posted by AtW View PostLook at HTML -
<div align="center"><img src="/cam/cam_1.jpg" name="refresh1" height="240" width="320" />
It appears I was right, as usual.

<div align="center"><img src="/cam/cam_1.jpg" name="refresh1" height="240" width="320" />
and cam 2 to;
<div align="center"><img src="/cam/cam_2.jpg" name="refresh2" height="240" width="320" />
On refreshing the page I get the correct two images for ten seconds then it reverts to the two identical images.
Leave a comment:
-
Look at HTML -Originally posted by Sysman View PostI can see 2 different webcam piccies on your site now. If I do an open new window on each image and inspect the url they are like this:
cam_1.jpg?1283612973061
cam_2.jpg
<div align="center"><img src="/cam/cam_1.jpg" name="refresh1" height="240" width="320" />
It appears I was right, as usual.
Leave a comment:
-
I can see 2 different webcam piccies on your site now. If I do an open new window on each image and inspect the url they are like this:
cam_1.jpg?1283612973061
cam_2.jpg
Leave a comment:
- Home
- News & Features
- First Timers
- IR35 / S660 / BN66
- Employee Benefit Trusts
- Agency Workers Regulations
- MSC Legislation
- Limited Companies
- Dividends
- Umbrella Company
- VAT / Flat Rate VAT
- Job News & Guides
- Money News & Guides
- Guide to Contracts
- Successful Contracting
- Contracting Overseas
- Contractor Calculators
- MVL
- Contractor Expenses
Advertisers
Contractor Services
CUK News
- Who owns the loans? Inside the mystery of the loan charge recall scandal Today 06:20
- Umbrella company winding-up petitions in 2026: the practical guide for contractors Yesterday 05:29
- Payments on Account deadline: what contractors must do before July 31st — maybe for the final few times Jul 28 08:01
- Andy Burnham's first 100 days: five things contractors need from the new PM Jul 27 00:53
- Starmer vs Burnham on housing: What their rival plans mean for your contractor mortgage Jul 22 00:59
- Burnham's housing vision vs. Starmer's home-buying reforms: what it means for your contractor mortgage Jul 22 00:59
- In Khalil v Innovate Transport, a limited company contractor wasn’t a worker and was on £2.30 — not £230 Jul 21 07:58
- Andy Burnham is PM: 5 new IT policies set to shape UK tech and its contractors Jul 20 06:29
- Taxed on money I haven't earned yet? Bold move, HMRC Jul 17 08:36
- The Fair Work Agency has got zero hours in its sights. Do you? Jul 16 08:44

Leave a comment: