Created
January 6, 2012 08:21
-
-
Save psiborg/1569661 to your computer and use it in GitHub Desktop.
JS Ajax
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function getStockQuote() { | |
| var xhr; | |
| if (window.XMLHttpRequest) { | |
| xhr = new XMLHttpRequest(); | |
| } | |
| else { | |
| xhr = new ActiveXObject("Microsoft.XMLHTTP"); | |
| } | |
| xhr.onreadystatechange = function () { | |
| if (xhr.readyState === 4 && xhr.status === 200) { | |
| var json = JSON.parse(xhr.responseText); | |
| //console.log(json); | |
| var html = '<table border="0" cellpadding="3" cellspacing="0">'; | |
| html += '<tr><td colspan="2"><h3>Stock quote for ' + json.symbol + '</h3></td></tr>'; | |
| html += '<tr><td>Last Trade:</td><td><strong>' + json.last_trade + '</strong></td></tr>'; | |
| html += '<tr><td>Change:</td><td>' + json.change + '</td></tr>'; | |
| html += '<tr><td>Prev. Close:</td><td>' + json.prev_close + '</td></tr>'; | |
| html += '<tr><td>Low - High:</td><td>' + json.day_low + ' - ' + json.day_high + '</td></tr>'; | |
| html += '<tr><td>Volume:</td><td>' + json.volume + '</td></tr>'; | |
| html += '<tr><td>Updated:</td><td>' + json.update_date + ' ' + json.update_time + '</td></tr>'; | |
| html += '</table>'; | |
| document.getElementById('stocks').innerHTML = html; | |
| } | |
| } | |
| xhr.open("GET", "stocks.php?sym=RIM.TO", true); | |
| xhr.send(); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment