Prompt user to change chain

Hello everyone,
I don’t use typescript / nodejs

i use pure javascript with web3

I would like to understand how to create a prompt with metamask for the user when connected with the wrong chain on my site.

exactly like pancakeswap does, if I’m on the ETH chain and connect to their site, a metamask prompt will ask me to switch to the BSC network.

I would like to post a picture or a link for you to see but I can’t do it on this forum, it’s madness

/**

 * agrega o cambia la red con el chainid que le mandes

 * si se le manda el chainid de la red que tiene el usario seleccionada no hace nada

 * @param {int} id es el chainid de la blockchain

 */

export async function addNetwork(id) {

  let networkData;

  switch (id) {

    //bsctestnet

    case 97:

      networkData = [

        {

          chainId: "0x61",

          chainName: "BSCTESTNET",

          rpcUrls: ["https://data-seed-prebsc-1-s1.binance.org:8545"],

          nativeCurrency: {

            name: "BINANCE COIN",

            symbol: "BNB",

            decimals: 18,

          },

          blockExplorerUrls: ["https://testnet.bscscan.com/"],

        },

      ];

      break;

    //bscmainet

    case 56:

      networkData = [

        {

          chainId: "0x38",

          chainName: "BSCMAINET",

          rpcUrls: ["https://bsc-dataseed1.binance.org"],

          nativeCurrency: {

            name: "BINANCE COIN",

            symbol: "BNB",

            decimals: 18,

          },

          blockExplorerUrls: ["https://testnet.bscscan.com/"],

        },

      ];

      break;

    default:

      break;

  }

  // agregar red o cambiar red

  return window.ethereum.request({

    method: "wallet_addEthereumChain",

    params: networkData,

  });

}

this code can’t use the defaults networks example ethereum mainnet, if you want realted stuff find EIP-3326

hi, thanks, really useful for BSC

as you said actually it doesn’t work for the main ethereum network.

there is no solution at the moment?