0

I have a situation where I am looking to make a script when the user loads the index.html file, to perform a browser check and to see if the user is using only Google Chrome. And if the user is using other browsers, to have an error like "sorry, you have to use google chrome". I have the code below:

<html>
.....
<body onload="load()">
...content
     <script type="text/javascript">
        function load() 
        {
          if (navigator.userAgent.search("MSIE") >= 0)
          { 
              document.write('This tool is required to be used only by Google Chrome.');
          }
          else if (navigator.userAgent.search("Edge") >= 0)
          {
              document.write('This tool is required to be used only by Google Chrome.');
          }
          else if (navigator.userAgent.search("Chrome") >= 0)
          {
              var url = document.URL, 
              index = url.indexOf("#"),
              hash = index != -1 ? url.substring(index+1) : "";
              if(hash === "")
              {
                location.hash = "#default";
              }
              else
              {
                var tabInfo = chrome.tabs.getCurrentTab();
                var id = tabInfo.tabId;
                window.location = id;         
              }
          }
          else if (navigator.userAgent.search("Firefox") >= 0)
          {
              document.write('This tool is required to be used only by Google Chrome.');
          }
          else if (navigator.userAgent.search("Safari") >= 0 && navigator.userAgent.search("Chrome") < 0)
          {
              document.write('This tool is required to be used only by Google Chrome.');
          }
          else if (navigator.userAgent.search("Opera") >= 0)
          {
              document.write('This tool is required to be used only by Google Chrome.');
          }
          else
          {
              document.write('This tool is required to be used only by Google Chrome.');
          }
        }  

</body>

The code itself works for every other browser, except IE(Internet explorer). The version of the IE is 11.3930.14393.0.

Can you guys help me out with this please?

  • 4
    If you only want them to use Google Chrome, why check the other browsers? Why not simply show your error message if they are not using Chrome? – Tony Drummond Nov 02 '20 at 01:38
  • Because they are still using it like this , but there are other scripts where only google chrome is supported and the result is gonna be to appear glitches and the actual script to do not work properly. The purpose of checking the browsers is to check if only google chrome is used, and if not the whole content is hidden. – Gabriel Brooks Nov 02 '20 at 01:40
  • Not following why you can't simply do something like if(!Chrome){ /// Sorry you cannot enter } else { //show Chrome only content} – Tony Drummond Nov 02 '20 at 01:45
  • Well the reason is because the other browsers are still able to open the script instead of showing up the error – Gabriel Brooks Nov 02 '20 at 01:52

0 Answers0