Reputation: 29
I have buildt an eth node in local PC. The code is no problem. But when I exited from eth node, and use metamask to run the code, web3 is undefined.
Can you tell me how to solute the problem?
<!DOCTYPE html>
<html>
<head>
<title>Using web3 API with MetaMask</title>
<script src="https://cdn.jsdelivr.net/gh/ethereum/web3.js/dist/web3.min.js"></script>
<!-- for ecrecover -->
<script type="text/javascript" src="D:\blockchain\test\ethereum\ethereumjs-util.js"></script>
<script>
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 {
console.log('No web3? You should consider trying MetaMask!')
window.web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:8545"));
}
</script>
</head>
</html>
Upvotes: 2
Views: 1916
Reputation: 3950
When your ethereum node is down, your website cannot get information from your node, you can switch to infura.io provider
window.web3 = new Web3(new Web3.providers.HttpProvider("https://mainnet.infura.io"))
Upvotes: 0
Reputation: 21
Are you trying to serve from file://
? If so, it will not work. MetaMask refuses to serve filesystem URLs. Serve your application via a web server. A one-liner such as python -m SimpleHTTPServer 8000
will do.
Upvotes: 2