You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何修复ActiveXObject实现的IE转Chrome跳转在Chrome中失效及运行时兼容问题

问题根因
  • 你当前使用的ActiveXObject是仅IE浏览器支持的私有API,Chrome、Firefox等现代浏览器均不支持该接口,所以在Chrome中运行代码必然失效
  • 原有shell.run("Chrome http://example.com")的写法本身兼容Chrome已运行场景,调用成功后会在已打开的Chrome窗口中自动新开标签页打开目标地址,之前在IE中运行没触发该效果大概率是Chrome路径未加入系统环境变量
可行解决方案

方案1:自定义URL协议(全浏览器兼容,推荐)

该方案不依赖ActiveX,所有浏览器都可正常触发,且天然兼容Chrome已运行场景:

  1. 先在Windows系统中添加自定义协议注册表项,你可以新建一个后缀为.reg的文件,写入以下内容后双击导入系统即可:
Windows Registry Editor Version 5.00

[HKEY_CLASSES_ROOT\OpenChrome]
@="URL:OpenChrome Protocol"
"URL Protocol"=""

[HKEY_CLASSES_ROOT\OpenChrome\shell]

[HKEY_CLASSES_ROOT\OpenChrome\shell\open]

[HKEY_CLASSES_ROOT\OpenChrome\shell\open\command]
@="cmd /c start chrome \"%1\""
  1. 将跳转页面代码修改为如下版本,IE、Chrome中打开均可正常触发跳转:
<html lang="en">
<head>
<meta charset="utf-8">
<title>Redirect</title>
</head>
<body>
<h1>Browser Redirect</h1>
<script type="text/javascript">
function pageRedirect() {
  // 兼容IE原有逻辑
  if(!!window.ActiveXObject || "ActiveXObject" in window) {
    var shell = new ActiveXObject("WScript.Shell");
    // 写Chrome全路径避免环境变量找不到的问题,稳定性更高
    shell.run('"C:\\Program Files\\Google\\Chrome\\Application\\chrome.exe" http://example.com');
  } else {
    // 非IE浏览器调用自定义协议唤起Chrome
    window.location.href = "OpenChrome://http://example.com";
  }
}
setTimeout(pageRedirect, 3000);
</script>
</body>
</html>

注:首次在非IE浏览器触发跳转时会弹出用户确认提示,用户同意后后续即可正常跳转,不需要重复确认。

方案2:兼容原有IE逻辑的优化版本(仅适用内网场景)

如果是内网使用,所有用户都已将站点加入IE受信任站点并开启了ActiveX执行权限,只需修复原代码的两个bug即可正常兼容Chrome运行态场景:

  1. 原代码varshell漏写空格,语法错误
  2. 未定义pageRedirect函数,setTimeout调用会直接报错
    修复后代码如下:
<html lang="en">
<head>
<meta charset="utf-8">
<title>Redirect</title>
</head>
<body>
<h1>Browser Redirect</h1>
<script type="text/javascript">
function pageRedirect() {
  try {
    var shell = new ActiveXObject("WScript.Shell");
    shell.run('"C:\\Program Files\\Google\\Chrome\\Application\\chrome.exe" http://example.com');
    window.close();
  } catch(e) {
    alert("跳转失败,请手动打开Chrome访问目标地址");
  }
}
setTimeout(pageRedirect, 3000);
</script>
</body>
</html>

内容的提问来源于stack exchange,提问作者user17444611

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.23 16:36:02