I’m facing a problem when I call an object from different files.
function node(id, content, prev, next){
this.id = id;
this.prev = prev;
this.next = next;
this.content = content;
}
I’m using this code to load the external file in case if the object is not defined before.
function callNode(node){
if(typeof(node) === 'undefined')
{
path = "js/tree/t2.js";
$.getScript(path, function(){
console.log('done');
for(i in node)
alert(node[i]);
});
}
else alert('node exist');
}
in the t2.js i have the following:
n1 = new node('text1','n1');
n2 = new node('text2','n2');
n3 = new node('text3','n3');
n2.next = n3;
n2.prev = n1;
the html code:
<button onclick="callNode(n2)"..
but it keeps giving me undefined object
You cannot call
callNode(n2)ifn2is uninitialized yet. You will need to load the script first, then you can accessn2– for example from a callback.