0

I used to use nodejs for working with web3js and it worked fine..But now I want to use vanilla javascript to connect with web3js. I used CDN <script src="https://cdn.jsdelivr.net/npm/web3@latest/dist/web3.min.js"></script>, but didn't worked though..

$(document).ready(function () {
  if (window.ethereum) {
    window.web3 = new Web3(window.ethereum);
    try {
      // ask user for permission
      ethereum.enable();
      // user approved permission
    } catch (error) {
      // user rejected permission
      console.log("user rejected permission");
    }
  } else if (window.web3) {
    window.web3 = new Web3(window.web3.currentProvider);
    // no need to ask for permission
  } else {
    window.alert(
      "Non-Ethereum browser detected. You should consider trying MetaMask!"
    );
  }
});

Output: Non-Ethereum browser detected. You should consider trying MetaMask!

PS: I have metamask installed

4
  • Instead of using document.ready, could you please try using $(function () { $(window).load(function () { //Your code here.... }); }); Nov 4, 2020 at 15:23
  • Nope, it's not working
    – Yash
    Nov 4, 2020 at 17:14
  • If possible could you please share your project code? Nov 5, 2020 at 16:20
  • @Yash is it resolve.. how? because I'm having same issue Aug 11, 2021 at 12:42

1 Answer 1

0

Try:

window.addEventListener('load', async () => {
    if (window.ethereum) {

    [...]

Your Answer

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

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