-1

There is very strange. It looks like after the If statement, the script it stop. The page has a javascript function is called by clicking the button. If the browser on IE, I will do something. Otherwise I close the page. I put alert statement to test it. However the alert has never fired. Would someone tell me what's wrong will my code.

There is my button call the function:

  <input class="btn" id="btnClose" onclick="javascript:openFile();" type="button" value="Close" name="btnClose" />   

There is the javascript function:

 function openFile() {

            var url = 'file://' + document.getElementById("hdURL").value; 
            //alert('Open File' + url);
            var location = document.getElementById("hdURL").value;

            ////http://stackoverflow.com/questions/9847580/how-to-detect-safari-chrome-ie-firefox-and-opera-browser
             if ((/*@cc_on!@*/false || !!document.documentMode) ||(!isIE && !!window.StyleMedia))
             {
                 alert('IE');
                //do something
                window.self.close();
             }
             alert('test');  //never fire   

             closeWindow();

        }
user819774
  • 1,456
  • 1
  • 19
  • 48
  • 3
    `'do something` - is this supposed to be a comment? If so, it should be `//do something`. – Tyler Roper Mar 17 '17 at 14:43
  • 1
    I don't see where `isIE` is defined. Look in your browsers console. Do you see errors there? –  Mar 17 '17 at 14:45
  • @Amy That's a good point. Taking a look at [the question OP referenced](http://stackoverflow.com/questions/9847580/how-to-detect-safari-chrome-ie-firefox-and-opera-browser) for their `if` logic, the accept answer uses `var isIE = ( ... logic ... )`. OP copy+pasted that logic and used it as the first condition of their `if` statement in the question above, rather than casting it to the var `isIE` like the referenced answer. – Tyler Roper Mar 17 '17 at 14:47
  • It is typo. If it is on IE, the alert('IE') is fire. When it is on firefox and Chorme, the alert('test') not fire. – user819774 Mar 17 '17 at 14:53
  • I wouldn't consider calling an undefined variable a "typo". IE hits true for your first condition so the second condition doesn't matter whatsoever. Chrome and Firefox on the other hand will get to that second condition and have no idea what `isIE` is. – Tyler Roper Mar 17 '17 at 14:53

2 Answers2

1

First off, going to give credit to Amy in the comments on the question for realizing that your isIE is not defined.


Take a look at the accepted answer to the question you referenced in your code. It says the following: (I've added an arrow to show the use of isIE)

// Internet Explorer 6-11
var isIE = /*@cc_on!@*/false || !!document.documentMode;
//    |________    
//             |
// Edge 20+    V
var isEdge = !isIE && !!window.StyleMedia;

Notice how their example for detecting edge references the variable for detecting IE, isIE. In your example, you don't have var isIE, and so this is coming back as undefined - the code fails.


Why does it work in IE but not Firefox/Chrome?

JavaScript will not evaluate the second condition of an OR if the first is true - this is known as Short Circuit Evaluation.

When using IE, the first condition evaluates to true. This means that the second condition (and the syntax error therein) is ignored.

However, Chrome and Firefox get false for the first condition, and must evaluate the second. Once they get to the undefined variable, an error will be thrown.


Solution:

// Internet Explorer 6-11
var isIE = /*@cc_on!@*/false || !!document.documentMode;

// Edge 20+
var isEdge = !isIE && !!window.StyleMedia;

if (isIE || isEdge) {
    //DO STUFF
}
Community
  • 1
  • 1
Tyler Roper
  • 21,445
  • 6
  • 33
  • 56
  • Not necessary. If I'd wanted the rep I could have answered myself. I'm happy that Santi answered and got some rep. Share the love. –  Mar 17 '17 at 17:11
0

it is always helpful to prepare jsifddle, so you can immediately test your solution and see errors. It is also good to check browser console which shows errors

see working example

https://jsbin.com/sipukovono/edit?html,css,js,console,output

function openFile() {

var isIE=false; // temporary definition

var url = 'file://' + document.getElementById("hdURL").value; 
        //alert('Open File' + url);
        var location = document.getElementById("hdURL").value;


         if ((false || !!document.documentMode) ||(!isIE && !!window.StyleMedia))
         {
             alert('IE');
            window.self.close();
         }
         alert('test');  //never fire   

         closeWindow();

    }
plusz
  • 224
  • 2
  • 16
  • I'm not sure saying "There's an error here, just set it to `false` and forget about it" is an elegant solution. – Tyler Roper Mar 17 '17 at 15:02
  • I suppose this variable will be set after checking what browser it is. Having working code in jsbin user can now make required modifications and tests. As we can see form original question it was not easy to determine that code is corrupted. – plusz Mar 17 '17 at 23:44