1

So I am having a problem with some of my code because the website I'm pulling my widget from, is down. What I have is a script that calls to a widget to pull up future projected values of oil prices. This morning the website I pull my information from is down, and I was wondering if there was a way to load up an alternative site if this one is down. Here is a snippet of my code:

<div class="item1">
  <span class="title">Brent Crude:</span>
  <script type="text/javascript" src="https://www.oilcrudeprice.com/oilwidget.php?l=en&m=000000&g=ffffff&c=ed3232&i=ffffff&l=e88888&o=ed7070&u=brent"></script>
</div>

I was wondering if there way a way to pull information from https://oilprice.com/widgets/brent/brentchart.js instead of www.oilcrudeprice.com when www.oilcrudeprice.com is down.

1 Answers1

0

You can always load an alternative js file by using the onError handler.

<script type="text/javascript" src="http://example.com/script1.js" onError="loadAlternative(this);"></script>

and the error handler function would be:

function loadAlternative(elem)
{
   elem.src = "http://alternative_address.com/somefile.js";
}
Shakti Phartiyal
  • 6,156
  • 3
  • 25
  • 46
  • Is there a way to speed up the error? It takes like 30+ seconds before I get the error kicked out to me because of the webpage timing out. – LordTakahiro Mar 28 '17 at 18:51
  • @LordTakahiro I understand your concern, but the timeout is how you actuaally get to know that the site is down. ;) . and getting the timeout takes time – Shakti Phartiyal Mar 28 '17 at 18:54