Permalink
Browse files

Fixed Provider check and Async Call

Check out my blog post here for in depth look into the changes:  http://shawntabrizi.com/crypto/ethereum/correcting-the-ethereum-and-web3-js-hello-world/
  • Loading branch information...
shawntabrizi committed Nov 5, 2017
1 parent a4f6226 commit daa8ac6c380c6f870807023e295d51a03a21edef
Showing with 21 additions and 11 deletions.
  1. +21 −11 index.html
View
@@ -1,20 +1,30 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<script type="text/javascript" src="./web3.min.js"></script>
<script type="text/javascript">
var Web3 = require('web3');
var web3 = new Web3();
web3.setProvider(new web3.providers.HttpProvider("https://mainnet.infura.io/<APIKEY>"));
window.addEventListener('load', function () {
if (typeof web3 !== 'undefined') {
console.log('Web3 Detected! ' + web3.currentProvider.constructor.name)
window.web3 = new Web3(web3.currentProvider);
} else {
console.log('No Web3 Detected... using HTTP Provider')
window.web3 = new Web3(new Web3.providers.HttpProvider("https://mainnet.infura.io/noapikey"));
}
})
function getBalance() {
var address = document.getElementById("address").value
var address, wei, balance
address = document.getElementById("address").value
try {
var wei = web3.eth.getBalance(address)
var balance = web3.fromWei(wei, 'ether');
document.getElementById("output").innerHTML = balance + " ETH";
}
catch (err) {
document.getElementById("output").innerHTML = err.message;
web3.eth.getBalance(address, function (error, wei) {
if (!error) {
var balance = web3.fromWei(wei, 'ether');
document.getElementById("output").innerHTML = balance + " ETH";
}
});
} catch (err) {
document.getElementById("output").innerHTML = err;
}
}
</script>
@@ -28,4 +38,4 @@ <h1>ETH Balance Fetcher</h1>
<br />
<div id="output"></div>
</body>
</html>
</html>

0 comments on commit daa8ac6

Please sign in to comment.