Search code examples
javascriptjquerystocks

Loading an alternative script if website widget located is down


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.


Solution

  • 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";
    }