Hey, I used code from tutorial of network tracking, but when network changes in metamask it doesn’t reload page.
var web3;
checkWeb3();
function displayMessage(messageType, message) {
messages = {
"00": `<div class= "alert alert-success"> ${message} </div>`,
"01": `<div class= "alert alert-danger"> ${message} </div>`,
};
document.getElementById("resultSpace").innerHTML = messages[messageType];
}
async function checkWeb3() {
const ethereum = window.ethereum;
if (!ethereum || !ethereum.on) {
displayMessage("01", "This App Requires MetaMask, Please Install MetaMask");
} else {
//displayMessage("00", "Metamask is Installed");
setWeb3Environment();
}
}
function setWeb3Environment() {
web3 = new Web3(window.ethereum);
getNetwork();
monitorNetwork();
}
async function getNetwork() {
var chainID = await web3.eth.net.getId();
console.log(chainID);
if (chainID == 137) {
displayMessage("00", "Correct Network");
} else {
displayMessage("00", "Wrong Network, please change it to Polygon Mainnet");
}
}
function monitorNetwork() {
Moralis.onChainChanged(function () {
window.location.reload();
});
}