Inject Web3

The Chrome plugin Metamask inject a global variable web3 in your browser. You can see it in your console browser, you have this message MetaMask - injected web3 and you can see the web3 object by taping web3 in your console. MetaMask is a proxy between the server side and your browser interface. Your program can see this variable only when it's loading with your browser.

Variable NameDescription
window.ethereumModern ethereum provider. Specified in EIP-1102 & EIP-1193.
window.web3Legacy ethereum provider.

Web3 Browsers

There are a list of web3 browsers that can be used to interact with decentralized application (Dapp):

  • Meta Mask
  • Opera
  • Status
  • Tenta Browser
  • Trust Wallet's Dapp Browser
  • Mist Browser (deprecated)

Access Dapp Via Web3 Browser

		<link href="../media/vendor/bootstrap/css/bootstrap.min.css" rel="stylesheet">
		<script src="[email protected]/dist/web3.min.js"></script>
		<div class="container-fluid pt-3">
			<button id='btn-connect' type='button' class='btn btn-primary'>Connect For More Information</button>
			<div id='access-info' style='display:none;'>
				<h5 class="mt-3" id='hashtag3'>Access Information</h5>
				<table class="table table-bordered">
					<tr><td style="width:20%;">Web3 Version</td><td id='access-web3ver'></td></tr>
					<tr><td style="width:20%;">Account(s)</td><td id='access-account'></td></tr>
					<tr><td>Provider</td><td id='access-provider'></td></tr>
	function getCurrentProvider() {
		if (!window.web3) return 'unknown';

		if (window.web3.currentProvider.isMetaMask)
			return 'metamask';

		if (window.web3.currentProvider.isTrust)
			return 'trust';

		if (window.web3.currentProvider.isGoWallet)
			return 'goWallet';

		if (window.web3.currentProvider.isAlphaWallet)
			return 'alphaWallet';

		if (window.web3.currentProvider.isStatus)
			return 'status';

		if (window.web3.currentProvider.isToshi)
			return 'coinbase';

		if (typeof window.__CIPHER__ !== 'undefined')
			return 'cipher';

		if ( === 'EthereumProvider')
			return 'mist';

		if ( === 'Web3FrameProvider')
			return 'parity';

		if ( &&'infura') !== -1)
			return 'infura';

		if ( &&'localhost') !== -1)
			return 'localhost';

		return 'unknown';
	async function showAccounts(acc) {
		const accounts = await web3.eth.getAccounts();
		document.getElementById('access-account').innerHTML = accounts.join(", ");
		document.getElementById('access-provider').innerHTML = getCurrentProvider();
		document.getElementById('access-web3ver').innerHTML = window.web3.version;
		document.getElementById("access-info").style.display = "block";
	document.getElementById("btn-connect").addEventListener('click', async () => {
		//Modern web3 browsers
		if (window.ethereum) {
			window.web3 = new Web3(window.ethereum);		
			try {
				await window.ethereum.request({ method: 'eth_requestAccounts' }).then(
			} catch (error) {
		//Legacy web3 browsers
		}else if (window.web3) {
			window.web3 = new Web3(web3.currentProvider);
			// Acccounts always exposed
		//Non web3 browsers
		else {
			if (window.self !== { //is in iframe
				//trust wallet has problem with content in iframe, so here is to load the content in new page.
				if(confirm("Provider not found. This could be a reason which some web3 browsers are not able to run content in iframe. Press 'OK' to try this page out of iframe.")) {"eth_js_access_inject_web3.html", '_blank');
			} else {
				alert('Your browser is not supported with web3. Please try this in web3 browser or install metamask as browser extension!');
	<script type="text/javascript" src="../media/vendor/iframeresizer/js/iframeResizer.contentWindow.min.js"></script> 

About Us
Contents have been open source in GITHUB. Please give me a ⭐ if you found this helpful :)
Problem? Raise me a new issue.
Support Us
Buy me a coffee. so i can spend more nights for this :)

BTCSCHOOLS would like to present you with more pratical but little theory throughout our tutorials. Pages' content are constantly keep reviewed to avoid mistakes, but we cannot warrant correctness of all contents. While using this site, you agree to accept our terms of use, cookie & privacy policy. Copyright 2019 by BTCSCHOOLS. All Rights Reserved.