-1

I have this code which hides certain HTML code when using different browser. Here is the code:

<script type="text/javascript">
    $(document).ready(function () {
    var is_chrome = !!window.chrome && !is_opera;
    var is_explorer= typeof document !== 'undefined' && !!document.documentMode && !isEdge;
    var is_firefox = typeof window.InstallTrigger !== 'undefined';
    var is_safari = /^((?!chrome|android).)*safari/i.test(navigator.userAgent);
    var is_opera = !!window.opera || navigator.userAgent.indexOf(' OPR/') >= 0;

    if(is_chrome || is_firefox)
    {
      $('#otherbrowser').hide();
    }
    else
    {
      $('#logOut').hide();
    }

 });
</script>

It works when i use safari and Google Chrome but when i tried using Internet Explorer it does not hide the #otherbrowser. What seems to be the problem? Any help would be appreciated. Thanks.

Eddie
  • 26,593
  • 6
  • 36
  • 58
Ronald Torres
  • 199
  • 2
  • 4
  • 19
  • 1
    Because your logic only hides the element if it is chrome or firefox. – Nick.Mc Apr 13 '18 at 03:38
  • May be your logic to determine if browser is IE, not working as you expecting. Have a look https://stackoverflow.com/questions/19999388/check-if-user-is-using-ie-with-jquery – PM. Apr 13 '18 at 03:38
  • Look at the code. Regardless of whether `is_explorer` is correct... it's not even being checked – Nick.Mc Apr 14 '18 at 08:26

1 Answers1

0

Your code is missing the variable isEdge, that's the reason it's not working in IE. Just remove the isEdge condition or add the definition for that.

Also add the is_explorer in your if condition. So that it will hide the #otherbrowser section in IE.

 $(document).ready(function () {
    var is_chrome = !!window.chrome && !is_opera;
    var is_explorer= typeof document !== 'undefined' && !!document.documentMode && !isEdge;
    var isEdge = navigator.userAgent.indexOf('Edge') > 0;
    var is_firefox = typeof window.InstallTrigger !== 'undefined';
    var is_safari = /^((?!chrome|android).)*safari/i.test(navigator.userAgent);
    var is_opera = !!window.opera || navigator.userAgent.indexOf(' OPR/') >= 0;

    if(is_chrome || is_firefox || is_explorer)
    {
      alert("condition passed");
    }
    else
    {
      alert("condition failed");
    }

 });
Suresh Ponnukalai
  • 13,820
  • 5
  • 34
  • 54
  • Thanks for the comment bro.I appreciate your help. This is the code I used detect the IE browser and its works fine. ` if (/MSIE (\d+\.\d+);/.test(navigator.userAgent) || navigator.userAgent.indexOf("Trident/") > -1 ) { $('#logOut').hide(); }` – Ronald Torres Apr 13 '18 at 06:26
  • @RonaldTorres The code that you posted in the question doesn't take any notice of your `is_explorer` variable. The code posted in this answer _does_. What relevance is your comment here? – Nick.Mc Apr 15 '18 at 01:16