...

View Full Version : Error: array.splice is not a function, and more big fun



chump2877
12-12-2006, 06:36 PM
First, I'm experiencing an AgGrAvAtInG error on the following page: http://www.mediamogulsweb.com/client/parseXML/testingHTML.html


Error: cat[u].splice is not a function

The relevant code, with the line of code that is triggering the error in red:


function processGetPost2()
{
var myajax = ajaxpack.ajaxobj;

if (myajax.readyState == 4)
{
// if request of file completed

if (myajax.status == 200 || window.location.href.indexOf("http") == -1)
{
// if request was successful or running script locally
// Update catalog code here

var dataNew2 = myajax.responseText;
var arrKey2 = parseInt(dataNew2) - 1;

var h = 0;
for (u=0; u<catNameArray.length; u++)
{
if (cat[u] == sortBy)
h = 1;

cat[u].splice(arrKey2,1);

tt = 0;
for (var w in cat[u])
{
cat[u][tt].childNodes[0].nodeValue = cat[u][w].childNodes[0].nodeValue;
tt++;
}

if (h == 1)
{
sortBy = cat[u];
h = 0;
}
}

if (cat[0].length == 0)
{
alert('The catalog is empty!');
return false;
}

if (matches.length > 0)
{
if (matches.length > recsPerPage)
{
if ((matches.length - 1) % recsPerPage == 0)
currentSearchPage = currentPage - 1;
}

searchXML(search_id);
}
else
{
records = sortRecords();
myPages = paginate();

if (records.length >= recsPerPage)
{
if (records.length % recsPerPage == 0)
currentPage -= 1;
}

displayPage(currentPage,'yes');
}

clearTimeout(timer);

var childnode=document.getElementById('workingBox');
var removednode=document.getElementById('main_content').removeChild(childnode);
}
else
{
//alert('myajax.status='+myajax.status);
}
}
else
{
//alert('myajax.readyState='+myajax.readyState);
}
} Any ideas? I feel like I've tried everything...

Second (when the above problem is resolved), I'm also getting an error in the following code:


function popupBox()
{
window.onscroll = function()
{
if (window.pageYOffset)
document.getElementById('workingBox').style.top = window.pageYOffset + 145 + 'px';
else if (document.documentElement.scrollTop)
document.getElementById('workingBox').style.top = document.documentElement.scrollTop + 145 + 'px';
else
document.getElementById('workingBox').style.top = document.body.scrollTop + 145 + 'px';
}

timer = setTimeout('popupBox()',1000);
}

The error says something to the effect of:


document.getElementById('workingBox') has no properties.

The error is triggered when the scroll bar is used after a record is deleted. I used clearTimeout to clear the timer, so I can;t understand why the window.onscroll event continues to be triggered after I kill the timer (and presumably popupBox() stops executing recursively?)...

I must be missing something here...

Anyway, could use some help with both of these problems...thanks

A1ien51
12-12-2006, 08:19 PM
is cat[u] an array?

if you do alert(typeof cat[u]); what does it say?

Eric

chump2877
12-12-2006, 09:12 PM
is cat[u] an array?

if you do alert(typeof cat[u]); what does it say?cat[u] should be an object of type Array...

when I execute the alert(), it returns "object".....

how exactly do I interpret that?

Does the script forget that cat[u] is an Array when returning from my PHP script?

The array is initially created here onLoad:


function extractData()
{
var entiredoc = xmlDoc.documentElement;
var docnodes = entiredoc.childNodes;
parentNode = docnodes[1].tagName;

for (q=0; q<catNameArray.length; q++)
{
cat[q] = xmlDoc.getElementsByTagName(catNameArray[q]);
}

showCatalog(cat[0],catNameArray[0]);
}

getElementsByTagName should return an array object? It is recognized as such everywhere else in my script, just not in this particular AJAx callback function...

david_kw
12-12-2006, 10:28 PM
Actually from what I read getElementsByTagName() does not return an array but an "array like" object.

the difference is in the definition

var arr = [];
var arr_like = {};

After that they are addressed similar

arr[0] = 6;
arr_like[0] = 6;

unless you try and launch an array specific function.

If you have JavaScript : The Definitive Guide check page 122.

david_kw

chump2877
12-12-2006, 10:41 PM
http://www.byteclub.net/wiki/index.php?title=Javascript_getElementsByTagName

I assume that's what you are referring to....

chump2877
12-12-2006, 11:03 PM
well anyway that was worth the agony...NOT..lol

thanks david AGAIN for steering me in the right direction...:rolleyes:

this modified code appears to allow me to operate on the HTMLcollection exactly like an array, as opposed to somewhat like an array (who invented these so-called "HTMLcollections"?!?....phhht):


function extractData()
{
var entiredoc = xmlDoc.documentElement;
var docnodes = entiredoc.childNodes;
parentNode = docnodes[1].tagName;

for (q=0; q<catNameArray.length; q++)
{
cat[q] = collectionToArray(xmlDoc.getElementsByTagName(catNameArray[q]));
}

showCatalog(cat[0],catNameArray[0]);
}

// Convert HTMLCollection object to Array object

function collectionToArray(col)
{
a = new Array();
for (i=0; i<col.length; i++)
a[a.length] = col[i];
return a;
}

chump2877
12-12-2006, 11:05 PM
By the way, I still could use a hand with this, and now you can actually see the error for yourself:


Second (when the above problem is resolved), I'm also getting an error in the following code:


function popupBox()
{
window.onscroll = function()
{
if (window.pageYOffset)
document.getElementById('workingBox').style.top = window.pageYOffset + 145 + 'px';
else if (document.documentElement.scrollTop)
document.getElementById('workingBox').style.top = document.documentElement.scrollTop + 145 + 'px';
else
document.getElementById('workingBox').style.top = document.body.scrollTop + 145 + 'px';
}

timer = setTimeout('popupBox()',1000);
}

The error says something to the effect of:


document.getElementById('workingBox') has no properties. The error is triggered when the scroll bar is used after a record is deleted. I used clearTimeout to clear the timer, so I can;t understand why the window.onscroll event continues to be triggered after I kill the timer (and presumably popupBox() stops executing recursively?)...

I must be missing something here...

liorean
12-12-2006, 11:16 PM
this modified code appears to allow me to operate on the HTMLcollection exactly like an array, as opposed to somewhat like an array (who invented these so-called "HTMLcollections"?!?....phhht):I think they were invented because of a slew of different things they needed to do:
- They're document structures, so the methods for array modifications can't work on them. Insertion, deletion, replacement, sorting, joining, folding, mapping can't work the same way as they do on arrays.
- They're representations of live documents. If a document changes for any reason, they change as well.
- They're effectively read only.
- And of course, they're made to work from both JavaSript and Java, as well as other languages.

chump2877
12-12-2006, 11:26 PM
I think they were invented because of a slew of different things they needed to do:
- They're document structures, so the methods for array modifications can't work on them. Insertion, deletion, replacement, sorting, joining, folding, mapping can't work the same way as they do on arrays.
- They're representations of live documents. If a document changes for any reason, they change as well.
- They're effectively read only.
- And of course, they're made to work from both JavaSript and Java, as well as other languages.

I knew there was a reason! :) Well this would be nice, as quoted from http://www.byteclub.net/wiki/index.php?title=Javascript_getElementsByTagName

"It would be nice if the collectionToArray function above could be added to HTMLCollection's prototype, however for some reason it is read-only."

BTW, still need a little help with my second problem....Weeeeee...I'm silly...:rolleyes:

chump2877
12-13-2006, 12:48 AM
Nevermind, second problem has been resolved...thanks for everyone;s help...:thumbsup:



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum