Page is a not externally linkable
- Code, Content, and Presentation
-- JavaScript and AJAX
---- scope


Fotiman - 2:57 pm on Feb 17, 2012 (gmt 0)


The problem is that you are setting myid AFTER the call to displayProjects. Therefore, when displayProjects is executed, myid is still undefined. This will work:


function jsfunctions_args(){
var d = document;
// set myid BEFORE calling displayProjects
myid = d.getElementById("args");

//function call with arguments
displayProjects("Bluebeam", "Artichoke", "Monarch", "Starfish", "PaperClip", "Naomi", "Butterfly");

function displayProjects(){
for(var i = 0; i < displayProjects.arguments.length; i++){
mytn = d.createTextNode(displayProjects.arguments[i]);
write_results();
}
}
}


With that said, this approach is not really a very good approach. You have a bunch of global variables being created, and it's not easy to debug where each is getting set (as you've found). It's also a good practice to declare every variable using "var", so you know where the scope is. Also, your function definition appears in the file below the call to the function. That call happens from inside another function which is called externally (after the entire script has loaded), so it will work, but I generally prefer to put the definitions first in the file so that it's easier to find them, but also if you ever need to call that function from within that file before the entire script has been processed, then you'll need it to be defined first.

I think there's a lot of room for improvement in this script.


Thread source:: http://www.webmasterworld.com/javascript/4418326.htm
Brought to you by WebmasterWorld: http://www.webmasterworld.com