Is there code to auto reload images if it doesn't load at first time in users browser ?
is there code to auto reload images if it doesn't loaded first time in users browser?
Collapse
X
-
-
hi Thanks for reply
as dormilich said maybe not loaded because connection timeout or apache server capacity that cause 503 error , but it all solves if the user refresh it , but i need a code to ( auto refresh the website if user got 503 apache error ) ( and auto reload the image if i didn't loaded in user's browser )
is there something like that ?Comment
-
Hey.
You could use the onerror event to try to reload the image.
[code=html]
<img src="some_image .png" onerror="reload Image(this);" />[/code]
[code=javascript]
function reloadImage(pTh is) {
// To prevent this from being executed over and over
pThis.onerror = null;
// Refresh the src attribute, which should make the
// browsers reload the iamge.
pThis.src = pThis.src;
}[/code]
Even tho the event is not a part of any standard, it works in every browser since IE5.5.Comment
-
Thanks Dear Atli
is the second code Javascript ?
could it be dynamically so i don't have to add the line onerror="reload Image(this); in every image path ?Comment
-
the final code would be like this ?
Code:var images = document.getElementsByTagName('IMG'); for (var i in images) { onerror="reloadImage(this);" }
Comment
-
No, you would have to use the elements of the images array, like kovik's comment said, and add the onerror event on there.
[code=javascript]
images[i].onerror = function() {
this.onerror = null;
this.src = this.src;
}[/code]
And note that this has to be executed after every <img> element you want it to work on, but before the window.onload event. Putting it just before the closing </body> tag would probably be best.
is it working with images that in CSS background ?Comment
-
Thanks Dear , but i think i'm little confused , i wish you have Largeness to correct if something wrong
is this the correct code to add to javascript in header
Code:<script type="text/javascript"> var images = document.getElementsByTagName('IMG'); for (var i in images) { images[i].onerror = function() { this.onerror = null; this.src = this.src; } } </script>
so i must add something in <img ... > tag ?
which code i should put before </body> , if is this javascript code so i must put it in header to work ?
is there any example for how DOM event works ? may it would help in css background images
i'm sorry maybe it's easy for others but i'm still learning and in beginning level , i appreciate helpingComment
-
could make any difference if i put the code in header instead ?
and is the final code above correct ?
sorry i didn't get meaning by ( this has to be executed after every <img> )
so i must add something in <img ... > tag ?
is there any example for how DOM event works ? may it would help in css background imagesComment
-
i thought that the browser reading the page from the <head> first then read <body>
before i use the code kindly can you tell me this complete code is correct or there's something wrong ?
Code:<script type="text/javascript"> var images = document.getElementsByTagName('IMG'); for (var i in images) { images[i].onerror = function() { this.onerror = null; this.src = this.src; } } </script>
Comment
Comment