Website with JS doesn't work in IE9 until the Developer Tools is activated

前端 未结 8 1373
情歌与酒
情歌与酒 2020-12-12 15:53

I\'m developing a complex website that heavily leverages jQuery and a number of scripts. On load of the site, none of my scripting is working (though I can confirm that othe

8条回答
  •  遥遥无期
    2020-12-12 16:07

    I have run into this issue many times. Basically with variables we do this to check if they are valid

    var somevar;
    if (somevar)
     //do code
    

    this works because somevar will resolve to undefined. But if your checking a window property for example. window.console.

    if (console) <---- this throws an exception
    

    You cannot do the same check. The browser treats it differently. Basically only doing this

    if (window.console) <---- will NOT throw an exception if undefined
    //some code
    

    this will work the same as the first example. So you need to change your code to

    function log(msg){
     if (window.console){
         console.log(msg);
     }
    }
    

提交回复
热议问题