admin管理员组

文章数量:1530060

IE打开chrome浏览器脚本

window.onload = function () {
        // var objShell = new ActiveXObject("WScript.Shell");
        // objShell.Run("chrome www.baidu", 0, true);

        // filePath 根据实际情况填写
        var href = "http://www.baidu";
        var objShell = new ActiveXObject("WScript.Shell");
        var newObj1 = new ActiveXObject("Scripting.FileSystemObject");
        var cmd = "";
        // let filePath = "C:\\app\\Chrome-81\\chrome.exe";
        var filePath = "C:\\Program Files(x86)\\Google\\Chrome81\\Application\\chrome.exe";
        if (newObj1.FileExists(filePath)) {
          console.log("y");
          cmd =
            'cmd.exe /c start C:\\Program" "Files" "(x86)\\Google\\Chrome81\\Application\\chrome.exe ' +
            url;
        } else {
          cmd = "cmd.exe /c start chrome " + href;
        }

        objShell.run("cmd.exe /c " + cmd, 0, true);
}

上面这个脚本在页面加载的时候自动打开chrome浏览器,前提你电脑已经安装了chrome浏览器。

如果你想打开火狐浏览器,方法思路跟这个是一样的,改下Run里面的命令。

当我们在IE浏览器运行上述脚本是,会发现没有任何反应。

按F12,我们发现控制台出现这个:

Automation 服务器不能创建对象

解决上面这个报错,需要进行IE的一些配置,步骤如下:

 配置完成后重启IE浏览器,再次访问我们的脚本,发现可以正常弹出一个chrome窗口!

本文标签: 控件器中浏览器jsActiveX