I have this script working with jquery and uploadify
$('#uploader').uploadify({
'uploader' : '/admin/includes/uploadify/uploadify.swf',
'script' : '/admin/includes/uploadify/uploadify_storage.php',
'scriptData': {'sessionId': sessionId},
'cancelImg' : '/admin/includes/uploadify/cancel.png',
'buttonImg' : '/site_images/add_files.png',
'folder' : storage,
'auto' : false,
'multi' : true,
'fileExt' : '*.jpg',
'simUploadLimit' : 2,
'wmode': 'transparent',
'width': '150',
'height': '20',
'removeCompleted': false,
'queueID' : 'upload_queue',
'onComplete' : function(event, ID, fileObj, name,response, data) {
console.log(path);
console.log(sub_ul);
}
});
//code//
$("li.dir > span.name").live('click', function(){
$("span").removeClass("selected");
var li = $(this).parent();
var root = $(li).attr("title");
var folder = $(li).find("span.name").html();
var path = root+"/"+folder;
path = path.replace('//', '/');
var sub_ul = $(li).find("> ul.sub_folder");
set_path(sub_ul);
$(this).addClass("selected");
});
basically when I you click a span with .name it runs the code getting var sub_ul = $(li).find("> ul.sub_folder"); which works (its sent to set_path() ) . In a second time when I launch uploadify at the end on OnComplete the console.log(path); shows the correct value but console.log(sub_ul); returns nothing (an alert too) like the object is not specified. I can’t use objects after the JS has run? Don’t they stay saved in the DOM like path does?
In theonCompletefunction that you’ve posted, you’re loggingul_suband notsub_ulas you seem to want to.Unless in
set_path()you set the value for a global variable namedpath, I don’t see how you would get the same value in those two different functions – the variablepathwithin theclickhandler is scoped to that function and will not be accessible elsewhere. The same goes forsub_ul.I’m not quite sure what to make of this, JavaScript objects are not ‘saved in the DOM’. If you’re unable to access an object you’ve created, then the context must be different from the one the object was created in – it must now be out of scope and thus inaccessible. Using closure is one way to keep functions and variables accessible. Using global vars is another way to do it but is not recommended and should be avoided unless absolutely necessary.
You might want to read these articles about scope in JavaScript to get a better idea of how things work:
You’ve mentioned in the comments that you’ve made a global
pathvariable but not asub_ul. One simple way to ‘fix’ your code is to use global variables. But again, use of global variables is generally discouraged since you could end up polluting the global namespace.And without seeing more of your code, it’s hard to say if this will always work correctly – for instance, your uploadify script might be triggered by events that do not cause the values of
pathandsub_ulto be set, which would mean that when theonCompletefunction runs, it’ll get the wrong values for both.