html代码如下:
<html>
<head>
<meta charset="utf-8">
<title>Fist App Demo</title>
<link rel="stylesheet" type="text/css" href="main.css">
<script src="https://cdn.staticfile.org/jquery/2.0.0/jquery.min.js"></script>
<script src="https://cdn.jsdelivr.net/gh/ethereum/web3.js/dist/web3.min.js"></script>
</head>
<body>
<div class="container">
<h1>Web3.js 0.20 Demo Dapp Demo</h1>
<h2 id="info"></h2>
</div>
<script>
if (typeof web3 !== 'undefined') {
web3 = new Web3(web3.currentProvider);
console.log(1)
} else {
// set the provider you want from Web3.providers
web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:8121"));
console.log(2)
}
console.log('web3 version',web3.version)
// 查看账户信息
web3.eth.getAccounts(console.log);
</script>
</body>
</html>
启动geth:
geth --networkid "12" --rpc --rpcport "8121" --rpccorsdomain "*" --datadir node0 --port "8545" --nodiscover --ipcdisable console
火狐浏览器控制台输出:
2 web3_0.20.html:25:21
web3 version 1.2.6 web3_0.20.html:27:21
(2) […]
0: "0x6045ff8EAeC8be1B75Fae4F7D9dD55a9838d599D"
1: "0x46dDAC9996Ae42e2DA25eFe460A794A1c7D69280"
length: 2
<prototype>: Array [] web3.min.js:1:459693