2022年10月27日 星期四

JS判斷瀏覽器IE自動轉換Edge或Chrome

 

  1. 提示IE不支援, Edge開啟. IE導向其他頁面

<script>

    if (/MSIE \d|Trident.*rv:/.test(navigator.userAgent)) {

        alert("IE is not supported.");

        window.location = 'microsoft-edge:' + location.href;

       

        setTimeout(function () {

            window.location.href = 'https://localhost';

            //window.close();

        }, 1);

    }

</script>

 

  1. 直接轉Chrome開啟, IE導向其他頁面

<script>

    if (/MSIE \d|Trident.*rv:/.test(navigator.userAgent)) {

       

        var objShell = new ActiveXObject("WScript.Shell");

        objShell.Run("cmd.exe /c start "  + location.href, 0, true);

        setTimeout(function () {

            //history.go(-2);

            window.location.href = 'https://localhost';

            //window.close();

            }, 1);

    }

</script>

沒有留言: