The Javascript code is working correctly in Firefox and Chrome, but for IE(I am using 11),it's not working until I hit F12 for debugging. And there is also no error displayed in debugger in IE. Any idea why?
The first function is to check if the browser support classList or not, apparently, IE doesn't support it. Here is the code:
function checkClassListSupport() {
var supportsClassList = ({}).toString.call(document.body.classList) == "[object DOMTokenList]";
return supportsClassList;
}
It's working now if I remove the console.log code, or change the document module to "Edge".
Here's my code to over-ride console.log(), and works well in Firefox, Chrome, Opera, etc.
var _log = console.log.bind(console);
window.console.log = function (data)
{
_log.call(this,data);
//do something
}
But, latest version of Microsoft Edge throws me an error.
SCRIPT445: Object doesn't support this action
(at line 1 - var _log = console.log.bind(console); to be specific)
How can I make it work on Edge? Why this doesn't work?
When surfing SO about this, saw this answer and cleared all my Edge settings and data. It worked.
I've tried to setup a testing environment with mocha, phantomjs and istanbul (and grunt). It works great so far, but when it comes to angular-testing i got some problems. I want (and need) to use angular-mocks, but as soon as I include it in my test.html, I get the following console-error in my browser:
Uncaught TypeError: (window.beforeEach || window.setup) is not a function
The matching code in angular-mocks.js is the following one:
(window.beforeEach || window.setup)(function() {
annotatedFunctions = [];
currentSpec = this;
});
This happens both for the current version (1.4.3) and for an old version which apparently worked in another project: 1.3.15.
What am I missing?
I fixed it.
The problem was, that i included my librarys (including angular-mocks.js) before mocha.js which apparently doesn't work.
Is there a console logger for IE? I'm trying to log a bunch of tests/assertions to the console but I can't do this in IE.
You can access IE8 script console by launching the "Developer Tools" (F12). Click the "Script" tab, then click "Console" on the right.
From within your JavaScript code, you can do any of the following:
<script type="text/javascript">
console.log('some msg');
console.info('information');
console.warn('some warning');
console.error('some error');
console.assert(false, 'YOU FAIL');
</script>
Also, you can clear the Console by calling console.clear().
NOTE: It appears you must launch the Developer Tools first then refresh your page for this to work.
Since version 8, Internet Explorer has its own console, like other browsers. However, if the console is not enabled, the console object does not exist and a call to console.log will throw an error.
Another option is to use log4javascript (full disclosure: written by me), which has its own logging console that works in all mainstream browsers, including IE >= 5, plus a wrapper for the browser's own console that avoids the issue of an undefined console.
Extremely important if using console.log() in production:
if you end up releasing console.log() commands to production you need to put in some kind of fix for IE - because console is only defined when in F12 debugging mode.
if (typeof console == "undefined") {
this.console = { log: function (msg) { alert(msg); } };
}
[obviously remove the alert(msg); statement once you've verified it works]
See also 'console' is undefined error for Internet Explorer for other solutions and more details
There is Firebug Lite which gives a lot of Firebug functionality in IE.
Simple IE7 and below shim that preserves Line Numbering for other browsers:
/* console shim*/
(function () {
var f = function () {};
if (!window.console) {
window.console = {
log:f, info:f, warn:f, debug:f, error:f
};
}
}());
In his book, "Secrets of Javascript Ninja", John Resig (creator of jQuery) has a really simple code which will handle cross-browser console.log issues. He explains that he would like to have a log message which works with all browsers and here is how he coded it:
function log() {
try {
console.log.apply(console, arguments);
} catch(e) {
try {
opera.postError.apply(opera, arguments);
}
catch(e) {
alert(Array.prototype.join.call( arguments, " "));
}
}
For IE8 or console support limited to console.log (no debug, trace, ...) you can do the following:
If console OR console.log undefined: Create dummy functions for
console functions (trace, debug, log, ...)
window.console = {
debug : function() {}, ...};
Else if console.log is defined (IE8) AND console.debug (any other) is not defined: redirect all logging functions to console.log, this allows to keep those logs !
window.console = {
debug : window.console.log, ...};
Not sure about the assert support in various IE versions, but any suggestions are welcome.
You can use cross-browser wrapper: https://github.com/MichaelZelensky/log.js
For older version of IE (before IE8), it is not straight forward to see the console log in IE Developer Toolbar, after spending hours research and trying many different solutions, finally, the following toolbar is great tool for me:
http://www.my-debugbar.com/wiki/CompanionJS/Installing
The main advantage of this is providing a console for IE6 or IE7, so you can see what are the error (in the console log)
Note:
It is free
screen shot of the toolbar
I've been always doing something like this:
var log = (function () {
try {
return console.log;
}
catch (e) {
return function () {};
}
}());
and from that point just always use log(...), don't be too fancy using console.[warn|error|and so on], just keep it simple. I usually prefer simple solution then fancy external libraries, it usually pays off.
simple way to avoid problems with IE (with non existing console.log)
How do I overwrite the global Exception handler in JavaScript so that it becomes the top level handler for all uncaught exceptions?
window.onerror didn’t work. The code is:
<HTML>
<HEAD>
<script language='javascript'>
window.onerror = function (em, url, ln) {
alert(em + ", " + url + ", " + ln);
return false;
}
function fGo() {
try
{
var a = b; // Error here: b not defined
}
catch (e)
{
throw e;
}
}
</script>
</HEAD>
<BODY>
<button onclick='fGo()'>GO</button>
</BODY>
</HTML>
I'm testing on Chrome, by the way. The developer console registers the uncaught exception, but the alert() in window.onerror does not appear.
As of 2013, Chrome supports the window.onerror. (I have version 25, and comments imply earlier versions as well.)
I wrapped jQuery using currying to create a proxy that always does a try...catch in the jQuery functions.
I use it in www.js-analytics.com. However, the solution only holds for jQuery scripts.
Before 2013 Google Chrome didn't support window.onerror, and apparently it wasn't implemented in WebKit.
window.onerror = function(errorMsg, url, lineNumber) {
// Code to run when an error has occurred on the page
}
Chrome support for window.onerror
I believe support started in Chrome v10 (Chromium Issue 7771), but it looks as if "full" support with CORS support was resolved around Chrome 30+ (Chromium Issue 159566)
caniuse.com doesn't currently track this JavaScript feature (GitHub Issue 11999) ... to add support to this issue, log in to GitHub and "react" with a "Thumbs Up" on the original post (don't +1 in comments).
Current Google documentation for window.onerror
Chrome DevTools ~ Handle runtime exceptions using window.onerror
Perhaps you're looking for window.onerror. I am not sure whether this is available on all browsers.