Forked from henrik/yahoo_exchange_rates_jsonp.html
Created
September 8, 2016 17:17
-
-
Save rafaelfoster/748e5302885d1fe69c6c6e69f21d5c8b to your computer and use it in GitHub Desktop.
JavaScript to get currency exchange rates from Yahoo Finance as JSONP. No XHR!
This file contains 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
Get exchange rate as JSONP via YQL. | |
YQL Console: http://developer.yahoo.com/yql/console | |
Query (USD to SEK): select rate,name from csv where url='http://download.finance.yahoo.com/d/quotes?s=USDSEK%3DX&f=l1n' and columns='rate,name' | |
Example code: | |
<script type="text/javascript"> | |
function getRate(from, to) { | |
var script = document.createElement('script'); | |
script.setAttribute('src', "http://query.yahooapis.com/v1/public/yql?q=select%20rate%2Cname%20from%20csv%20where%20url%3D'http%3A%2F%2Fdownload.finance.yahoo.com%2Fd%2Fquotes%3Fs%3D"+from+to+"%253DX%26f%3Dl1n'%20and%20columns%3D'rate%2Cname'&format=json&callback=parseExchangeRate"); | |
document.body.appendChild(script); | |
} | |
function parseExchangeRate(data) { | |
var name = data.query.results.row.name; | |
var rate = parseFloat(data.query.results.row.rate, 10); | |
alert("Exchange rate " + name + " is " + rate); | |
} | |
getRate("SEK", "USD"); | |
getRate("USD", "SEK"); | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment