Question

I am new to java-scripting and I shouldn't be doing this in first place. But I was trying out the following http://api.jquery.com/ready/

<!DOCTYPE html>
<html>
<head>
<style>p { color:red; }</style>
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script>
$(document).ready(function ()
    {
        $("p").text("The DOM is now loaded and can be manipulated.");
    }
);
</script>
</head>
<body>
<p>Not loaded yet.</p>
</body>
</html>

The above script works as expected. But I tried to replace the script with the following, expecting magic to happen(stock quote pulled in to the page). But I get a "Not loaded yet" message. Could someone point out what could be going wrong(or too many things done wrong) ?

stock quotes with javascript

<script type="text/javascript" src="jquery-1.5.1.js"></script>
<script type="text/javascript">
    $(document).ready(function() {
            var symbol='goog';
            var callback = function(data) {
                    var price=data.query.results.span[0].content;
                    alert('Stock Price: ' + price);
                };

            var url = 'http://query.yahooapis.com/v1/public/yql';
            // this is the lovely YQL query (html encoded) which lets us get the stock price:
            // select * from html where url="http://finance.yahoo.com/q?s=goog" and xpath='//span[@id="yfs_l10_goog"]'
            var data = "q=select%20*%20from%20html%20where%20url%3D%22http%3A%2F%2Ffinance.yahoo.com%2Fq%3Fs%3D" + symbol + "%22%20and%20xpath%3D'%2F%2Fspan%5B%40id%3D%22yfs_l10_" + symbol + "%22%5D'&format=json";
            $.getJSON(url, data, callback);
    });
</script>

this is how my code looks like:

<!DOCTYPE html>
<html>
<head>
<style>p { color:red; }</style>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script type="text/javascript">
    $(document).ready(function() {
            var symbol='goog';
            var callback = function(data) {
                    var price=data.query.results.span[0].content;
                    //alert('Stock Price: ' + price);
                    //$("p").price;
                    $("p").text("The DOM is now loaded and can be manipulated.");
                };

            var url = 'http://query.yahooapis.com/v1/public/yql';
            // this is the lovely YQL query (html encoded) which lets us get the stock price:
            // select * from html where url="http://finance.yahoo.com/q?s=goog" and xpath='//span[@id="yfs_l10_goog"]'
            var data = "q=select%20*%20from%20html%20where%20url%3D%22http%3A%2F%2Ffinance.yahoo.com%2Fq%3Fs%3D" + symbol + "%22%20and%20xpath%3D'%2F%2Fspan%5B%40id%3D%22yfs_l10_" + symbol + "%22%5D'&format=json";
            $.getJSON(url, data, callback);
    });
</script>
</head>
<body>
<p>Not loaded yet.</p>
</body>
</html>
Was it helpful?

Solution

Rather than:

 var data = "q=select%20*%20from%20html%20where%20url%3D%22http%3A%2F%2Ffinance.yahoo.com%2Fq%3Fs%3D" + symbol + "%22%20and%20xpath%3D'%2F%2Fspan%5B%40id%3D%22yfs_l10_" + symbol + "%22%5D'&format=json";

Do this: it's more maintainable:

var data = $.param({q:'select * from html where url="http://finance.yahoo.com/q?s=' + symbol + '" and xpath="//span[\'@id=yfs_l10_' + symbol + '\']"',format:'json'});

The response I got:

{"query":{"count":95,"created":"2013-06-14T17:57:32Z","lang":"en-US","results":{"span":[{"class":"yucs-notif-panel-title","content":"Notifications"},"Search",{"class":"title","content":"Breaking News"},{"class":"close","content":"×\n            "},{"class":"offscreen","content":"Close this window"},"HOME","INVESTING","NEWS","PERSONAL FINANCE","MY PORTFOLIOS","EXCLUSIVES","CNBC","\n                      \n                    ","\n                        \n                      ",{"input":{"id":"btnQuotes","type":"submit","value":"Get Quotes"}},{"id":"yfs_market_time","content":"Fri, Jun 14, 2013, 1:57PM EDT - U.S. Markets close in 2 hrs 3 mins"},{"id":"yfs_ad_"},{"a":{"href":"/q?s=%5EDJI","content":"Dow"}},{"id":"yfs_pp0_^dji","img":{"alt":"Down","border":"0","class":"neg_arrow","height":"14","src":"http://l.yimg.com/os/mit/media/m/base/images/transparent-1093278.png","style":"margin-right:-2px;","width":"10"},"strong":{"class":" yfi-price-change-down","content":"0.77%"}},{"a":{"href":"/q?s=%5EIXIC","content":"Nasdaq"}},{"id":"yfs_pp0_^ixic","img":{"alt":"Down","border":"0","class":"neg_arrow","height":"14","src":"http://l.yimg.com/os/mit/media/m/base/images/transparent-1093278.png","style":"margin-right:-2px;","width":"10"},"strong":{"class":" yfi-price-change-down","content":"0.69%"}},"Ticker Tape:",{"id":"tt_on","style":"display:inline;","content":"On"},{"id":"tt_off","style":"display:none;","content":"Off"},{"class":"rtq_exch","content":"\n                    NasdaqGS"},{"class":"rtq_dash","content":"-"},{"class":"time_rtq_ticker"},{"id":"yfs_l84_intc","content":"24.90"},{"class":"down_r time_rtq_content"},{"id":"yfs_c63_intc","img":{"alt":"Down","border":"0","class":"neg_arrow","height":"14","src":"http://l.yimg.com/os/mit/media/m/base/images/transparent-1093278.png","style":"margin-right:-2px;","width":"10"},"content":"\n                     0.09"},{"id":"yfs_p43_intc","content":"(0.36%)"},{"class":"time_rtq"},{"id":"yfs_t53_intc"},{"id":"yfs_t53_intc","content":"1:57PM EDT"},{"class":"add-to-portfolio-button","a":{"class":"add-to-portfolio-icon","href":"http://edit.finance.yahoo.com/ec?.src=quote&.intl=us&.sym=INTC&.done=http%3A%2F%2Ffinance.yahoo.com%2Fq%3Fs%3DINTC"}},"Add to Portfolio",{"id":"yfs_b00_intc","content":"24.88"},{"id":"yfs_b60_intc","content":"3800"},{"id":"yfs_a00_intc","content":"24.89"},{"id":"yfs_a50_intc","content":"18800"},{"id":"earn_cal_div","a":{"onclick":"window.open('http://calendar.yahoo.com/?v=60&ST=20130717&TITLE=INTC Earnings announcement&VIEW=d&REM1=7d&REM2=1d', 'earnwin', 'toolbars=no,scrollbars=no')","img":{"alt":"INTC Earnings announcement","class":"earn_cal_icon","id":"earn_cal_img","src":"http://l.yimg.com/os/mit/media/m/base/images/transparent-1093278.png","title":"INTC Earnings announcement"}}},"\n                        \n                      ",{"id":"yfs_g53_intc","content":"24.84"},"\n                        \n                      ",{"id":"yfs_h53_intc","content":"25.14"},"19.23","27.75",{"id":"yfs_v53_intc","content":"16,120,889"},{"class":"small","content":"(3m)"},{"id":"yfs_j10_intc","content":"123.78B"},{"class":"small","content":"(ttm)"},{"class":"small","content":"(ttm)"},{"a":[{"href":"/q?s=MSFT&reco=1","strong":"MSFT"},{"href":"/q?s=CSCO&reco=1","strong":"CSCO"},{"href":"/q?s=AMD&reco=1","strong":"AMD"},{"href":"/q?s=QCOM&reco=1","strong":"QCOM"},{"href":"/q?s=PFE&reco=1","strong":"PFE"},{"href":"/q?s=DELL&reco=1","strong":"DELL"}]},{"id":"trdnow_success"},{"class":"broker-head","content":"Featured Brokers"},{"class":"resetlink","a":{"href":"#","id":"reset_brkr","content":"Reset"}},{"class":"err_msg","id":"trdnow_err"},"(Fri 1:28PM EDT)","(Fri 1:04PM EDT)","(Fri 1:00PM EDT)","(Fri 10:25AM EDT)","(Fri 10:13AM EDT)","(Fri 10:08AM EDT)","(Fri 10:01AM EDT)","(Fri 9:30AM EDT)","(Fri 9:04AM EDT)","(Fri 6:16AM EDT)","(Fri 6:13AM EDT)","(Fri 5:10AM EDT)","(Thu, Jun 13)","(Thu, Jun 13)","(Thu, Jun 13)","(Tue, Jun 11)","(Tue, Jun 11)","(Mon, Jun 10)","(Jun 15)","(Jun 14)","(Jun 14)",{"id":"yfs_ad_"},{"id":"yfs_ad_"},{"id":"yfs_pp0_intc","img":{"alt":"Down","border":"0","class":"neg_arrow","height":"14","src":"http://l.yimg.com/os/mit/media/m/base/images/transparent-1093278.png","style":"margin-right:-2px;","width":"10"},"strong":{"class":" yfi-price-change-down","content":"0.36%"}},{"id":"yfs_j10_intc","content":"123.78B"},{"id":"yfs_pp0_arm.l","img":{"alt":"Up","border":"0","class":"pos_arrow","height":"14","src":"http://l.yimg.com/os/mit/media/m/base/images/transparent-1093278.png","style":"margin-right:-2px;","width":"10"},"strong":{"class":" yfi-price-change-up","content":"0.58%"}},{"id":"yfs_j10_arm.l","content":"12.04B"},{"id":"yfs_pp0_intc.mx","img":{"alt":"Up","border":"0","class":"pos_arrow","height":"14","src":"http://l.yimg.com/os/mit/media/m/base/images/transparent-1093278.png","style":"margin-right:-2px;","width":"10"},"strong":{"class":" yfi-price-change-up","content":"0.39%"}},{"id":"yfs_j10_intc.mx","content":"1.57T"},{"id":"yfs_pp0_tsmn.mx","img":{"alt":"Down","border":"0","class":"neg_arrow","height":"14","src":"http://l.yimg.com/os/mit/media/m/base/images/transparent-1093278.png","style":"margin-right:-2px;","width":"10"},"strong":{"class":" yfi-price-change-down","content":"4.13%"}},{"id":"yfs_j10_tsmn.mx","content":"1.20T"},{"id":"yfs_pp0_4335.hk","strong":{"class":" yfi-price-no-change","content":"0.00%"}},{"class":"label","content":"Sector"},{"class":"label","content":"Industry"},{"id":"yfs_ad_"},{"id":"yfs_ad_"},{"id":"yfs_params_vcr","style":"display:none","content":"{\"yrb_token\" : \"YFT_MARKET_WILL_CLOSE\", \"tt\" : \"1371232652\", \"s\" : \"intc\", \"k\" : \"a00,a50,b00,b20,b30,b60,c10,c60,c63,c64,c81,c82,c85,c86,g00,g53,h00,h53,j10,l10,l84,l85,l86,l90,o40,o50,p20,p40,p43,p44,t10,t50,t53,t54,v00,v53,z08,z09\", \"o\" : \"4335.hk,arm.l,intc.mx,tsmn.mx,^dji,^ixic,intc,^ixic,^gspc,^dji,^tnx,^vix,^rut,EURUSD=X\", \"j\" : \"c10,c63,j10,l10,l84,p20,p43,t10,t53,v00\", \"e\" : \"1\", \"version\" : \"1.0\", \"market\" : {\"NAME\" : \"U.S.\", \"ID\" : \"us_market\", \"TZ\" : \"EDT\", \"TZOFFSET\" : \"-14400\", \"open\" : \"\", \"close\" : \"\", \"flags\" : {}} , \"market_status_yrb\" : \"YFT_MARKET_WILL_CLOSE\" , \"portfolio\" : { \"fd\" : { \"txns\" : [ ]},\"dd\" : \"\",\"pc\" : \"\",\"pcs\" : \"\"}, \"SINGLE_QUOTE_PAGE\" : { \"symbol\" : \"INTC\",\"name\" : \"Intel Corporation\"}, \"STREAMER_SERVER\" : \"http://streamerapi.finance.yahoo.com\", \"DOC_DOMAIN\" : \"finance.yahoo.com\", \"localize\" : \"0\" , \"throttleInterval\" : \"1000\" , \"arrowAsChangeSign\" : \"true\" , \"up_arrow_icon\" : \"http://l.yimg.com/a/i/us/fi/03rd/up_g.gif\" , \"down_arrow_icon\" : \"http://l.yimg.com/a/i/us/fi/03rd/down_r.gif\" , \"up_color\" : \"green\" , \"down_color\" : \"red\" , \"pass_market_id\" : \"0\" , \"mu\" : \"1\" , \"lang\" : \"en-US\" , \"region\" : \"US\" , \"ticker_currency_sym\" : \"USD\" }"},{"id":"yfs_ad_"}]}}}

OTHER TIPS

I would imagine that there is something wrong with that huge query string you are building.

If you try to just go to that site in your browser with goog substituted in the right places:

http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20html%20where%20url%3D%22http%3A%2F%2Ffinance.yahoo.com%2Fq%3Fs%3Dgoog%22%20and%20xpath%3D'%2F%2Fspan%5B%40id%3D%22yfs_l10_goog%22%5D'&format=json

You will see that the site fails to load. Make sure you can reach the site in your browser first, then make sure that matches up with your code.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top