-1

The variable in $pieces[12] is the url of an image on my server, some times when the page loads there is a broken image. My script is suppose to find the broken image error if there is one and replace it with $pieces[12] again. If that sounds right. But I don't know how to do it with my code.

<div class="right_ad" id="right_ad">
  <script>
       <img src="<?php echo $pieces[12]; ?>" 
            onError="this.onerror=null;this.src='<?php echo $pieces[12]; ?>';" 
            style="width: 100%;max-height: 100%"/>
  </script> 
</div>
Saqueib
  • 3,484
  • 3
  • 33
  • 56
  • 1
    You could use jquery to replace src attrubute of the img tag. http://stackoverflow.com/questions/554273/changing-the-image-source-using-jquery – Deshan Jan 28 '15 at 06:28
  • Php Runs on server whereas javascript runs on client so I don't think what you are asking is achievable through PHP. You can use jquery as mentioned by @DeshanR – Muhammad Bilal Jan 28 '15 at 06:30
  • What about having JavaScript capture/copy $pieces[12] when the page loads. Then if there is a error JavaScript places in the url from $pieces[12] into img src=''? – user3456687 Jan 28 '15 at 06:35
  • Why would it work better the second time than the first time? If the URL is correct, then the image will load. If you're having network outages that cause your image to break, this is not something you should be trying to fix at a Javascript level. Why would the network be more reliable the second time? How often are you going to try? – deceze Jan 28 '15 at 06:47

2 Answers2

0

You should urlencode those values. My tip is that there are special values in your filenames.

Stephan Ahlf
  • 3,310
  • 5
  • 39
  • 68
0
function onError_run_this_func(){
//get your new image url with ajax function
//or if you already have it in $pieces[12] then use it.

$.('.image-class).attr('src', '<?php echo $pieces[12]; ?>');
}
<div class="right_ad" id="right_ad"><script><img class="image-class" src="`<?php echo $pieces[12]; ?>" onError="onError_run_this_func()" style="width: 100%;max-height: 100%"/></script> </div>`
Yasitha
  • 901
  • 2
  • 17
  • 42
  • 1
    Best answer so far it has been working with no broken image tags. Thanks for the fast answer would defiantly vote you up if I could hope others do. – user3456687 Jan 28 '15 at 07:51