I have build 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>
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.
当以太坊节点关闭时,您的网站无法从该节点获取信息,您可以切换到infura.io提供程序
window.web3 = new Web3(new Web3.providers.HttpProvider("https://mainnet.infura.io"))
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.