Created
October 16, 2017 16:00
-
-
Save spalladino/61b961f731dde03816b68b3680acaf6b to your computer and use it in GitHub Desktop.
Detect whether a web3 connection is available
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
// Adapted from https://github.com/MetaMask/faq/blob/master/DEVELOPERS.md#partly_sunny-web3---ethereum-browser-environment-check | |
window.addEventListener('load', function() { | |
// Checking if Web3 has been injected by the browser (Mist/MetaMask) | |
if (typeof web3 !== 'undefined') { | |
// Use Mist/MetaMask's provider | |
window.web3 = new Web3(web3.currentProvider); | |
} else { | |
// fallback - use your fallback strategy (local node / hosted node + in-dapp id mgmt / fail) | |
window.web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:8545")); | |
} | |
// Now you can start your app & access web3 freely: | |
startApp() | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment