Using jQuery to get share price is throwing up error, help!

Using jQuery to get share price is throwing up error, help!

Hello,

We are using a bit of jQuery to access the share price for a company on Yahoo Finance but I keep intermittently getting this error:

  1. Uncaught TypeError: Cannot read property 'quote' of null index.html:48
    1. (anonymous function) index.html:48
    2. o.fireWith jquery.min.js:2
  2. d.onload.d.onreadystatechange


    Sometimes it loads and sometimes not. The odd thing is that the data code for the company has changed recently from this:  http://uk.finance.yahoo.com/q?s=CPZ.AX&ql=1 to this  http://uk.finance.yahoo.com/q?s=SPZ.AX&ql=1 and I'm wondering whether that is causing the issue or if it's an error with the code.

    This is the code in the head:

    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
        <script>window.jQuery || document.write('<script src="/scripts/libs/jquery-1.7.1.min.js"><\/script>')</script>  

        <script type="text/javascript" src="//use.typekit.net/ewa5pge.js"></script>
        <script type="text/javascript">try{Typekit.load();}catch(e){}</script>
        
    <script>
        
        $(document).ready(function() {
            var $ticker = $('#ticker');
            if ($ticker.length == 1) {
        
                var tickerStatement = 'select '+$ticker.attr('data-pricefield')+', Change from yahoo.finance.quotes where symbol in ("'+$ticker.attr('data-symbol')+'")';
                
                var yqlQuery = $.ajax({
                    url: "http://query.yahooapis.com/v1/public/yql",
                    dataType: "jsonp",
                    data: {
                        q: tickerStatement,
                        format: "json",
                        env: 'store://datatables.org/alltableswithkeys'
                    //    callback: "?"
                    }
                });
                
                yqlQuery.success(function(response) {
                    var quote = response.query.results.quote;
                    
                    var changeDirection = 'change-none';
                    if (quote.Change > 0) {
                        changeDirection = 'change-increase';
                    } else if (quote.Change < 0) {
                        changeDirection = 'change-decrease';
                    } else {
                        quote.Change = '&mdash; &nbsp;No change';
                    }
                    
                    $('#ticker-price').html(quote[$ticker.attr('data-pricefield')]);
                    $('#ticker-change').html(quote.Change).addClass(changeDirection);
                    $('#ticker').removeClass('hidden');            
                });
            }
            
        });

    </script>

    This is the code on page:

    <div id="ticker-outer">
            <a href="http://www.asx.com.au/asx/research/companyInfo.do?by=asxCode&amp;asxCode=SPZ" target="_blank" class="png" id="ticker" data-symbol="SPZ.AX" data-pricefield="PreviousClose">
            <span id="ticker-top-text">SPZ: <strong>$AU</strong></span>
            <strong><span id="ticker-price"></span></strong>
            <span id="ticker-change" class="change-none"></span>
            <span id="ticker-change-checked">as of last close</span>
            </a>
           </div>

    Any help would be greatly appreciated as the site has just gone live with a dodgy ticker!!!

    Many thanks,

    Deb