firebug console.table returning undefined - javascript

I am trying to view the contents of a 2 dimensional array. I found out about using console.table() here. Every time I call console.log in the console I get undefined. I have tried running the site's example code
var table1 = new Array(5);
for (var i=0; i<table1.length; i++)
table1[i] = [i+1, i+2, i+3, i+4, i+5, i+6, i+7];
console.table(table1);
as well as calling console.table on my own arrays while paused in the debugger.
Any thoughts on how I can get this to work?
Edit: undefined is also returned when I just type console.log("abc"); in the console.
Edit2: reinstalling firebug seems to have fixed the issue.

Related

Javascript isn't setting the Array

I am not getting any errors. When you look at the console.log()'s outputs, the first one gives me the array, and when I open up the 2nd one, it says the array is empty! It's almost as if it was never set. The array I'm checking for is .axes.
I can directly access it, but then I can't see it when I expand the object.
This is for the most important thing in my life and the universe is trying to stop me.
for(i=0; i<$.count; i+=1){
h = chain[i];
h.axes = [ chain[0] ];
if (i==3){
console.log('Direct_AXES',h.axes);
console.log('Direct_H',h);
}
chain[0].dna.push(h);
}
As far as I can help you. This works like a charm.
var chain = [{},{},{},{},{}];
for(i=0;i<chain.length;i+=1){
h = chain[i];
h.axes = [ chain[0] ];
if (i==3){
console.log('Direct_AXES',h.axes);
console.log('Direct_H',h);
}
// chain[0].dna.push(h);
}
I added an array with four empty objects (so the code would somehow run). And I commented the last line of the loop because obviously I would run into an error.
Everything is working fine. So the conclusion here is that there is an error in the part you didn't post, because everything is working as expected.
Both logs do print the array and the whole object h containing the axes array.

Javascript issue on getelementbytagname

I'm having some trouble with this code:
var lista_input = document.getElementsByTagName("input");
console.log(lista_input);
console.log(lista_input[0]);
The first log correctly show me:
[item: function]
0: input
1: input
length: 2
__proto__: NodeList
but the second log show me:
undefined
I don't understand the reason,it could show me the first element input in dom!
The problem occurs from the fact that the return value of document.getElementsByTagName is a live NodeList:
var l = document.getElementsByTagName('div');
console.log(l[0]); // undefined
var d = document.createElement('div');
document.body.appendChild(d);
console.log(l[0]); // div
Combine that with the fact that you call the code before the document is ready (so before there are items in the list) and the known bug in the Console code that can show objects in a state after the call to console.log is made and you have the exact behavior you are experiencing.
To reiterate:
var lista_input = document.getElementsByTagName("input");
// lista_input is a live NodeList with 0 elements
console.log(lista_input); // will print the lista_input object at a later point
console.log(lista_input[0]); // will print undefined at a later point
/* time passes, dom is loaded */
// lista_input now has the inputs you expect it to have
/* time passes */
// logs are now shown in Console
EDIT: To get a good log, you can stringify the object when logging it, turning it into a primitive value that gets logged correctly:
var lista_input = document.getElementsByTagName("input");
console.log(JSON.stringify(lista_input)); // {"length":0} - empty list
console.log(JSON.stringify(lista_input[0])); // undefined
PS:
Link to a blog post explaining the Console bug:
http://techblog.appnexus.com/2011/webkit-chrome-safari-console-log-bug/
Link to a question requesting a fix to the Console bug:
How can I change the default behavior of console.log? (*Error console in safari, no add-on*)

Why is getElementsByTagName returning undefined?

I'm trying to call document.getElementsByTagName, and I'm getting back undefined as a result, no matter what parameter I pass. (Even if I pass "*".)
I tried Googling for it, but all the search results were about elements of the getElementsByTagName result array being undefined. What I'm getting is undefined as the result itself, and it's driving me up the wall.
Does anyone know what can cause this? (Using Firefox 12.0. In Chrome I get the expected results.)
EDIT: OK, here's sample code:
function buttonClick(){
var xhr = new XMLHttpRequest();
var msg = document.getElementById('message');
var buttons = document.getElementsByTagName("button");
var button, i;
for (i = 0; i < buttons.length; ++i){
button = buttons[i];
msg.removeChild(button);
}
xhr.onreadystatechange = function() {
if(xhr.readyState == 4){
handleResult(xhr.responseText, msg);
}
};
xhr.open("POST", location.href, true);
xhr.setRequestHeader("Content-type","application/x-www-form-urlencoded");
xhr.send("cmd=MyCommand");
}
And the getElementsByTagName always returns undefined, whether I trace it in Firebug's Script tab or call it from the Console tab. (Also in Firebug, since this seems to be confusing people. Apparently there are way too many consoles floating around.).
As proof, here's what I've been getting when I tried to use the Firebug console:
>>> document.getElementsByTagName("button");
undefined
>>> msg.getElementsByTagName("button");
undefined
>>> msg.getElementsByTagName
getElementsByTagName()
>>> msg.getElementsByTagName("BUTTON");
undefined
>>> msg.getElementsByTagName("*");
undefined
>>> document.getElementsByTagName("*");
undefined
>>> document.getElementsByTagName("body");
undefined
The markup is (or ought to be) irrelevant. It's a valid, well-formed HTML page with some buttons and other elements on it. This JS function is attached to the onclick of one of the buttons. But it looks something like this:
<html xmlns="http://www.w3.org/1999/xhtml"><head>
blah
</head>
<body>
<script type="text/javascript" src="/myJS.js"></script>
<div id="page-container">
<div id="message"><button onclick="buttonClick();">Button 1</button><button onclick="ButtonClick2()">Button 2</button></div>
</div>
</body></html>
edit:
This is a bug in firebug and is fixed by upgrading to 1.10.0a7
Because it is impossible for this method to return undefined, there are 2 possibilities:
Your debugging tools are lying to you
document.getElementsByTagName is not referencing the original host object. It should print
function getElementsByTagName() {[native code]} when referenced in console.
You should be able to reliably to see if it's in fact undefined (in firefox) with this:
delete window.alert;
window.alert(buttons);
The delete is a NOOP if window.alert is already referencing the original host object, otherwise
it will restore it.
If it alerts undefined, you should be able to do
delete document.getElementsByTagName
to restore the host object reference.
All console references here refer to the built in Web Console that comes with firefox by default.
I got the problem when I made a difficult to see syntax error. I used parenthesis when I should have used square brackets
Wrong:
selectedItem._element.childNodes(0).getElementsByTagName('input').item();
Right:
selectedItem._element.childNodes[0].getElementsByTagName('input').item();
See the difference? Note, the top syntax works is older versions of IE, like IE8, but it doesn't work in ie10, ie11, Edge etc
Isn't REPL a stand-alone, browser-independent JavaScript environment? While, in your case, in just happens to be running in your browser as a plugin, it's supposed to mimic a "clean room" per say...
To summarize this guy's answer: document.getElementById() returns null when using mozrepl (but not in firebug)
By default, you'r in the browser's context, not the document's.
Try this to switch to the document instead:
repl.enter(content)
You can't alert an Array, you should do a for loop to alert it. Example:
var x = document.getElementsByTagName('a');
for (var i = 0, c = x.length ; i < c ; i++) {
alert('Element n° ' + (i + 1) + ' : ' + x[i]);
}
This issue is also raised when we try to access element without array index. Because document.getElementsByTagName('tag') returns an array.

jQuery append fail in IE

I have this object that I would like to append to my div#doctors-list.
Firefox,Chrome work like a charm.But all IE fail. No errors are shown in the console.
$.each(sorteddoctorsArray[i2], function(idx, val) {
if ( !$.browser.msie ) {
$('div#doctors-list').append(val);
}else{
console.log(val);
// this logs [object Object]
$('div#doctors-list').append(val); // fails
}
});
any suggestions?
open it in IE and firefox to see the difference
try:
$('div#doctors-list').html($('div#doctors-list').html()+val);
It's hard to say when you disable the IE-Code(it currently is commented out).
But one issue I see so far(a few lines above the code posted by you) :
$('div#doctors-list').html('');
for(var i in priority){
for(var i2 in sorteddoctorsArray){
Both, priority and sorteddoctorsArray are native Arrays, you should never walk native Arrays by using for...in, always use for(var i=0;i<array.length;++i)
The for...in -Syntax will walk trough all members of an object. Also the build-in Array-members, e.g. length , will be fetched, what may result in errors.

Javascript TypeError: Cannot call method 'getSize' of undefined

I have been working with javascript lately and I am using Chrome (12) Developer tools and I have been getting the error:
TypeError: Cannot call method 'getSize' of undefined.
I have no idea of why this is happening but have isolated the part which seems to be the problem:
x = 0;
for(var i=0; i<w; i++){
for(var j=0; j<h; j++) {
display.blit(tile[world[x]], [(i * 34), (j * 34)])
x++;
}
}
the x++; give me the error and when I comment out the code (of x++;) chrome says there are no errors. Display.blit(tile[world[x]], [(i * 34), (j*34)] doesn't seem to have anything to do with the error although I may be wrong.
My question is how to fix this error and why it is happening in the first place.
The problem seems to be that your x exceeds the number of entries you have in your array. So as the entry is undefined you pass it along to your function where you call the getSize method which doesn't exist for undefined.
Without the rest of the script and the values that world and tile have it's hard to say where you went wrong but I would suggest you confirm that there is an entry for each of the calls you are making. If you do know how many entries you have just try alert(x) to see at which x value you get the error.

Categories

Resources