views:

36

answers:

1

I'm running some JavaScript via eval (I know, shoot me), that basically enumerates all of the properties on the document object. My issue is that while it works in firebug, it throws a not implemented exception in Firefox, when run from a script.

Link to JavaScript script, the exception thrown, and the firebug command working.

Any suggestions as to what's going on here?

For the record, this is done on Firefox 3.6.10 on Ubuntu 10.04 64-bit, and chrome does not have this issue.

+1  A: 

The error is here:

console.log(result);

remove that line and all should be fine.

The console object is a Firebug thing (refers to the Firebug console). Safari/Chrome just happen to also implement a console object (refers to Webkit js console). Firefox, indeed other browsers don't have a console object. So it throws an error.

BTW: As usual, the evals are completely unnecessary. This is exactly equivalent code:

for (key in document) {
    result[i] = typeof document[key];
    result[i+1]="document."+key;
    i+=2;
}

If you insist on calling it request then use it as a reference:

var request = window.document;
for (key in request) {
    result[i] = typeof request[key];
    result[i+1]=request+"."+key;
    i+=2;
}

If you insist on passing object names by string, then for sanity's sake use eval in a less confusing way:

var string = "window.document";
eval("var request ="+string);
for (key in request) {
    result[i] = typeof request[key];
    result[i+1]=request+"."+key;
    i+=2;
}

Though I wouldn't do even that (sometimes it is necessary, but only very rarely).

slebetman
The eval is necessary, as the object name is getting passed by string, and there's no way to change this. I will likely switch to your last recommendation however. But this doesn't solve the real issue, as if you look at the exception, its failing on line 53 of test.js which is the eval line, NOT the console.log line.
Michael
The line numbers for error messages in eval() calls on Firefox are incorrect.
johnjbarton
You can disable the Firebug Console panel (mini menu on the panel tab). Then reload and if the problem is window.console it will occur. Also if you have a string 'request' you can use for (var key in window[request]).
johnjbarton