I am trying to print web3.eth.<function> output on a html page, but I cannot print it.

I have followed this answer. My geth-node is running on --rpcport 8545.

Let's see simple example to print blockNumber.



    <script type='text/javascript'>
      window.addEventListener('load', function() {
          var web3 = window.web3 ;
          if (typeof web3 !== 'undefined') {
          web3 = new Web3(web3.currentProvider);
          } else {
          // set the provider you want from Web3.providers
          web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:8545"));

      function getblocknumber() {
           //document.getElementById("status").innerHTML =  web3.version.api //works, returns: 0.20.3
          document.getElementById("status").innerHTML = web3.eth.blockNumber; //returns empty string
 <p>Get Block Number:</p> <button onclick="getblocknumber()">Get Block Number </button>
 <p id="status"></p>


var express = require('express');
var app = express();
var bodyParser = require('body-parser');

// Create application/x-www-form-urlencoded parser
var urlencodedParser = bodyParser.urlencoded({ extended: false })


var server = app.listen(8081, function () {

  var host = server.address().address
  var port = server.address().port

  console.log("Example app listening at http://%s:%s", host, port)


To run:

$ node myserver.js
Example app listening at http://:::8081

To open; from browser: http://localhost:8081/example.html

2 Answers 2

web3.eth.getBlockNumber(function (err, blockNumber) {
    document.getElementById("status").innerHTML = blockNumber;
  • Could I also use same approach for events?
    – alper
    Oct 25, 2018 at 16:21
  • If you mean events emitted from your smart contract, you'd do something similar. (You'd subscribe and then update your UI in the callback.)
    – user19510
    Oct 25, 2018 at 16:32
  • Yes, I meant events emitted from your smart contract.
    – alper
    Oct 25, 2018 at 19:26

Which version of web3 are you using? If you are using v1.0.0 you are using it incorrectly...


  • Its v0.20.3 ...
    – alper
    Oct 25, 2018 at 19:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.