How to handle this Javascript error?

I’m not really sure what’s causing the problem. I have pages with javascript code just like this one. I use this to hide/show other component.

TB.render(‘component_8’, function(data) {
fieldValue = data.record.field_40;
if(fieldValue === “Seed”){
TB.hideComponent(‘component_9’);
TB.hideComponent(‘component_10’);
}
});

When I go to other page without this code from this page with the code, it is still running this code then causing an “Cannot read property ‘length’ of undefined” error. I guess it is because the page have the same component name/id.

Page A with javascript code has ‘component_8’.
Page B without code has ‘component_8’ as well.

How should I deal with this error?
Why is the code from the other page runs on the different page as well?

Hi @joshjosh, would you mind doing me a favor and seeing if this happens also after you refresh the page on “Page B” which does not contain the code?