1

I made a redirect link for my website from IE11 to Chrome by using ActiveXObject it works when I open the link in IE but when I open that redirect link in Chrome It doesn't work and is there a way to fix that problem.

<html lang="en">
<head>
<meta charset="utf-8">
<title> Redirect</title>
<h1>Browser Redirect</h1>
<script type="text/javascript">
  {
   varshell= new ActiveXObject("WScript.Shell");
   shell.run("Chrome http://example.com");
  }
  setTimeout("pageRedirect()",3000);
</script>
</head>
</html>
Stephen Ostermiller
  • 23,933
  • 14
  • 88
  • 109
  • Chrome does not, and never will, support ActiveXObject. This example will also only work in very limited IE11 cases and usually requires security setting modifications. (There are some Chrome add-ons that do, or did, by embedding IE in tabs; this is outside the scope of the question.) – user2864740 Dec 06 '21 at 06:39
  • "Normal" redirects, within the current browser, can be done merely by assigning to `window.location` - https://stackoverflow.com/q/503093/2864740 (there are a few variants, and some less-common alternatives) – user2864740 Dec 06 '21 at 06:44
  • Do you also want to redirect to another site in Chrome? If so, I think Stephen Ostermiller's answer is right. It will open Chrome and redirect to the site if you open the page in IE and directly redirect if you open the page in Chrome. – Yu Zhou Dec 07 '21 at 02:59

1 Answers1

1

You could have your pageRedirect function check if active X is available and choose how to redirect based on that:

function pageRedirect(){
  var url="http://example.com"
  if ("ActiveXObject" in window) new ActiveXObject("WScript.Shell").run("Chrome "+url);
  else location.href = url
}
setTimeout(pageRedirect,3000);

I've also changed a few other things:

  • You don't define the pageRedirect in your example code, so it doesn't look like your code would actually work.
  • setTimeout can take a function name as the first argument and run that function. It is usually preferable to do that than to pass it a string of code to execute.
  • You don't need to define the varshell variable, you can just call run directly on the new object that is constructed without assigning it to a variable.
Yu Zhou
  • 11,532
  • 1
  • 8
  • 22
Stephen Ostermiller
  • 23,933
  • 14
  • 88
  • 109
  • The standard redirect approach also works in IE11 (and before). Presumably the use of ActiveX is to “open chrome”. However ActiveX probably shouldn’t be used at all here and it will fail on public sites, even in IE (or when the WScript.run call otherwise fails). If IE really isn’t to be used, a banner (or other warning) is more generally suitable (fsvo) so the user can switch to a “modern” browser manually. – user2864740 Dec 06 '21 at 07:53