1

I would like to make a button (with a url) dynamic to what the user's browser is using. If the user is using Chrome, then the link will open in a new tab; however if the user is using IE, the link should open in Chrome.

I am unable to make the following code work in Chrome but this works in IE 9.

I have gotten helpful codes from some other posts.

The following will determine/ check which browser the user is on:

function getInternetExplorerVersion()
{
    var rv = -1; // Return value assumes failure.

    if (navigator.appName == 'Microsoft Internet Explorer')
    {
        var ua = navigator.userAgent;
        var re  = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})");
        if (re.exec(ua) != null)
            rv = parseFloat( RegExp.$1 );
    }

    return rv;
}

The above snippet works fine. My code below works seamlessly in IE but not in Chrome.

<input type="button" onclick="openURL()" value="Open Google">

<script>
  function openURL()
  {
      var ver = getInternetExplorerVersion();
      var shell = new ActiveXObject("WScript.Shell");

    if ( ver = -1 )
          shell.run("Chrome www.google.com");
        else
          window.open('www.google.com','_blank');
  }

</script>
oozmac
  • 113
  • 2
  • 16

1 Answers1

1

Chrome gives an error on that ActiveXObject line, so you should only expose that code to IE. Assuming that ver always gives a number other than -1 in IE you should try this:

function openURL() {
  var ver = getInternetExplorerVersion()

  if ( ver != -1 ) {
    // IE
    var shell = new ActiveXObject("WScript.Shell");
    shell.run("Chrome www.google.com");
  } else {
    // other browsers
    window.open('//www.google.com','_blank');
  }
}

Of course chrome was reading your code, you just didn't see the error. Press F12 to open the browser dev tools and go to the console tab to see script errors.

yezzz
  • 2,820
  • 1
  • 7
  • 18
  • btw, I just checked in IE, and `getInternetExplorerVersion()` also returns -1 in IE... so that code is not reliable. So it seems it only worked in IE because your if -statement was wrong. – yezzz May 07 '17 at 19:17
  • thank you so much for taking the time to solve this case. it works well in **CHROME**. you're the best – oozmac May 09 '17 at 12:37
  • You're welcome. Did you get the getInternetExplorerVersion function fixed? It did not give me correct ver in IE11. Also, the shell.run didn't work, but that's maybe an activeX problem. – yezzz May 09 '17 at 12:56
  • 1
    yes, for IE11, the string `Trident/` is now being used, you may refer to this [link](http://stackoverflow.com/questions/17907445/how-to-detect-ie11) – oozmac May 09 '17 at 13:11
  • last question, how do I do it if I want a link to open specifically in IE, just vice versa of my original question? hmmm – oozmac May 09 '17 at 13:12
  • 1
    If you mean click a link in chrome will be opened in IE... maybe it's possible somehow, but I don't know. I didn't even know about the IE trick to open link in another browser. I guess that's related to ActiveX. – yezzz May 09 '17 at 13:38