Skip to content

Instantly share code, notes, and snippets.

@spalladino
Created October 16, 2017 16:00
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save spalladino/61b961f731dde03816b68b3680acaf6b to your computer and use it in GitHub Desktop.
Save spalladino/61b961f731dde03816b68b3680acaf6b to your computer and use it in GitHub Desktop.
Detect whether a web3 connection is available
// 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