How to display currency exchange quote from Yahoo Finance with JavaScript



In this tutorial I want to show you how to retrieve currency exchange rate quote from Yahoo Finance using JavaScript.

This price quote is updated every second. The script is originally provided by: https://gist.github.com/henrik/265014

Here is the source code:

<DOCTYPE html>
<html>
	<head>
		<title>FXSpeculate</title>
	</head>
	<body>	
		<p id="gbpusd"></p>
		<script>
			var GBPUSD, GBPUSDrate;
			function parseExchangeRateGBPUSD(data) {
				GBPUSD = data.query.results.row.name;
				GBPUSDrate = parseFloat(data.query.results.row.rate, 10);
				document.getElementById("gbpusd").innerHTML =  "Rate for "  + GBPUSD + " is: " + GBPUSDrate;
			}
			function updateGBPUSD(){
				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%3DGBPUSD%253DX%26f%3Dl1n'%20and%20columns%3D'rate%2Cname'&format=json&callback=parseExchangeRateGBPUSD");
				document.body.appendChild(script);
			}
			updateGBPUSD();
			setInterval(function(){
				location.reload();
			}, 1000);
		</script>
	</body>
</html>
loading...