Uncaught ReferenceError web3 is not defined at window onload

0 votes

I am working on Windows 10, running a testrpc node via PowerShell. I used truffle to set up my folders & sample files, then compile and migrate.

Here is the code i am using:

var accounts;
var account;

function setStatus(message) {
  var status = document.getElementById("status");
  status.innerHTML = message;
};

function refreshBalance() {
  var meta = MetaCoin.deployed();

  meta.getBalance.call(account, {from: account}).then(function(value) {
    var balance_element = document.getElementById("balance");
    balance_element.innerHTML = value.valueOf();
  }).catch(function(e) {
    console.log(e);
    setStatus("Error getting balance; see log.");
  });
};

function calcPremium() {
    var premium = parseInt(document.getElementById("benefit").value)/10000;
    document.getElementById("monthlyPremium").innerHTML = "    Monthly Premium: $"+premium.toFixed(2);

};

function sendCoin() {
  var meta = MetaCoin.deployed();

  var amount = parseInt(document.getElementById("monthlyPremium").value);
  var receiver = document.getElementById("receiver").value;

  setStatus("Initiating transaction... (please wait)");

  meta.sendCoin(receiver, amount, {from: account}).then(function() {
    setStatus("Transaction complete!");
    refreshBalance();
  }).catch(function(e) {
    console.log(e);
    setStatus("Error sending coin; see log.");
  });
};

window.onload = function() {
  web3.eth.getAccounts(function(err, accs) {
    if (err != null) {
      alert("There was an error fetching your accounts.");
      return;
    }

    if (accs.length == 0) {
      alert("Couldn't get any accounts! Make sure your Ethereum client is configured correctly.");
      return;
    }

    accounts = accs;
    account = accounts[0];

    refreshBalance();
  });
}

I'm able to open the html file in a Chrome browser, with the MetaMask plugin enabled. However, it seems I'm unable to interact with the contracts in any way, due to the web3 error issue. 

Sep 28, 2018 in Blockchain by digger
• 26,740 points
2,392 views

1 answer to this question.

0 votes

I think the onload is giving the issue.

Try this code:

    $(window).load function() {
  web3.eth.getAccounts(function(err,accs) {
    if (err != null) {
      alert("There was an error fetching your accounts.");
      return;
    }

    if (accs.length == 0) {
      alert("Couldn't get any accounts! Make sure your Ethereum client is configured correctly.");
      return;
    }

    accounts = accs;
    account = accounts[0];

    refreshBalance();
  });
}
answered Sep 28, 2018 by slayer
• 29,370 points

Related Questions In Blockchain

+1 vote
2 answers

Integrating web3 from Metamask: "ReferenceError: window is not defined" error

You can't use MetaMask on server-side because ...READ MORE

answered Oct 10, 2018 in Blockchain by Omkar
• 69,220 points
7,006 views
0 votes
1 answer
0 votes
1 answer

TypeError: XMLHttpRequest is not a function when using web3 on meteor server side

I had the same problem. Instead ethereum:web3 Meteor package ...READ MORE

answered Oct 10, 2018 in Blockchain by Omkar
• 69,220 points
1,079 views
0 votes
1 answer

Command "network" is not defined. in hyperledger composer

For composer network to work, you must ...READ MORE

answered Nov 21, 2018 in Blockchain by Omkar
• 69,220 points
1,453 views
0 votes
1 answer

Truffle tests not running after truffle init

This was a bug. They've fixed it. ...READ MORE

answered Sep 11, 2018 in Blockchain by Christine
• 15,790 points
1,922 views
0 votes
1 answer

Hyperledger Sawtooth vs Quorum in concurrency and speed Ask

Summary: Both should provide similar reliability of ...READ MORE

answered Sep 26, 2018 in IoT (Internet of Things) by Upasana
• 8,620 points
1,451 views
+1 vote
1 answer

Protocols used in a distributed/dlt system for the nodes to establish communication

yes all are over TCP/IP connections secured ...READ MORE

answered Aug 6, 2018 in Blockchain by aryya
• 7,460 points
1,426 views
0 votes
1 answer

XMLHttpRequest is not defined while sending request to blockchain service via C#

use post data in url link and ...READ MORE

answered Sep 20, 2018 in Blockchain by slayer
• 29,370 points
1,199 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP