Created
March 3, 2013 23:08
-
-
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.
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
| <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> |
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
| <?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