-1

I need to test a JavaScript Project that uses console-object. So i downgrade from IE10 down to IE8 but i am not sure that my environment-testcases are "safe" for others.

This How can I use console logging in Internet Explorer? sais the console is undefined without Developertools(F12). But my current experience is that console.log is always available, no matter what F12 is active or not.

This article sais, they are fully enabled. This article sais console is undefined in IE8.

It looks like IE8 != IE8. Isnt it enougth to test pages in IE, Firefox, Opera and Safari?

<html>
<body>
<script>
document.write('Console is <b>' + typeof console + '</b>! <br />');
document.write('Even console.log is <b>' + typeof console.log + '</b>!<br />');
document.write(window.navigator.userAgent);
</script>
</body>
</html>

screenshot

Community
  • 1
  • 1
Grim
  • 4,441
  • 9
  • 51
  • 112

1 Answers1

1

Re console being undefined: This is true. IE8 and IE9 only define the console object when the F12 dev tools are opened. If the dev tools have not been opened, the console object is undefined and will throw fatal errors.

See also my answer to this question: Why does JavaScript only work after opening developer tools in IE once?

There are various ways around this, but the best solution is simply not to use the console object in production code -- the console object is not a web standard, so you should not rely on it existing (in fact there are other browsers in existence that don't provide a console object at all).

If you must use the console in your production code, you could create a shim for it when your page loads. Something like this:

if(!window.console) {window.console={log:function(){}};}

That should prevent it throwing fatal errors.

But really, the best answer is not to have console calls in your production code. It's intended as a developer tool, so it should only really be used in development code.

Community
  • 1
  • 1
Spudley
  • 161,975
  • 39
  • 229
  • 303
  • +1 I actually have devtools closed but have console defined anyway. The only solution i see is "dont use console in production". – Grim Jul 18 '13 at 14:00
  • The `undefined` point only holds before dev tools is opened at all; once dev tools has been opened and `console` is defined, it will remain defined even after dev tools is closed. But anyway, yes, I'd stick with the "don't use it in production" message, because it just isn't safe; as I say, there are browsers out there in real world use that don't have a console at all, and even among the ones that do have it, the functionality of the console varies from one browser to the next (and even between versions); as I say it isn't a defined standard, so the browser devs a free to basically make it up – Spudley Jul 18 '13 at 14:24
  • Trust me, i load the page without devtools, console is defined. i start devtools by f12, console is defined, i reload the page while devtools keeps open, console is defined, i close devtools, console is defined, i **reload webpage without devtools** console is defined. – Grim Jul 19 '13 at 07:39