[Ethereum] Web3.js browser version only

javascriptnodejsweb3js

I want to create a simple page to interact with my deployed contract. I don't want to use Node.js. I want to use html + web3.js browser version. I got a link: https://github.com/ethereum/web3.js/tree/develop/dist

But the above link contains web3.js files that contains "require" keyword which cannot run in browser. I want browser version of web3.js.

Best Answer

For web3.js 1.0, I got it working like this:

In head:

<script src="https://cdn.jsdelivr.net/gh/ethereum/web3.js@1.0.0-beta.34/dist/web3.min.js"></script>

Then get a reference to it:

<script>
var web3js = new Web3(new Web3.providers.HttpProvider('https://testnet-rpc.gochain.io'));
</script>

And use it like this:

var contract = new web3js.eth.Contract(abi, address);

Hope that helps!

Related Topic