Skip to content

Instantly share code, notes, and snippets.

@MidnightLightning
Created March 3, 2013 23:08
Show Gist options
  • Select an option

  • Save MidnightLightning/5078741 to your computer and use it in GitHub Desktop.

Select an option

Save MidnightLightning/5078741 to your computer and use it in GitHub Desktop.
Use AJAX to drop in a price in BTC on page load. If javascript fails, the page just displays the USD amount. It needs a PHP file to proxy the blockchain.info ticker, since it does not have Cross-Origin headers.
<html>
<head><title>BTC prices</title></head>
<body>
<p>Buy this widget for $10<span id="btc"></span>.</p>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script>
$(document).ready(function() {
$.ajax({
url: 'btc.php',
dataType: 'json',
success: function(data) {
console.log(data);
var usd = 10.00; // Value of the thing being sold
var ratio = data['USD']['24h']; // Current rate
var btc = usd/ratio;
btc = Math.round(btc*100000000)/100000000; // Round to 8 decimal places
$('#btc').html(' ('+btc+' BTC)'); // Insert the result
}
});
});
</script>
<?php
echo file_get_contents('http://blockchain.info/ticker');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment