Dear Javascript Gurus out There…

I have a bit of code that is using the excellent free XUpload progress bar. I’ve modified it a bit for my current project, and am having some performance issues due to some javascript I added.

What happens is the main <FORM> tag has an onSubmit() action of calling a StartUpload() function. I’ve added code to the start of the fuction to check to see if a delete file button has been pressed, and if it is the button and not the ‘upload’ button, it simply returns and doesn’t open the file transfer dialog.

My code looks like this (added code in green):


function StartUpload()
{
// first check if it’s a ‘delete_xxx’ button being hit, and return
for (var i=0;i<document.F1.length;i++)
{
current = document.F1.elements[i];
if(current.type==’submit’ && current.name.match(/delete_/))
{
return;
}
}

// … rest of function here…

It works, and it’s really slow, because I presume it goes through every single (HTML? or just form?) element on the page. What I really need is to find out what the current button that was pressed is. I know the name will be “delete_XX” where XX is a number from 01 to 05.

Can anyone help me out?

Update: I fixed this by digging a bit deeper into the code. In there there is a counter for the number of file browse dialogs that are not set to an empty string, so simply checking for if the counter is greater than 0 or not worked dandy. There is still an edge case where a large upload under mozilla gives the “Script is causing your computer to perform slowly” message, but I think that’s somewhere in my code.

3 Comments on “Dear Javascript Gurus out There…”

  1. If you believe this loop is taking a long time, pull it out. If you only have 5 possible fields that you want to check against, then do so. 5 compares will be much better than 50 (or however elements you have).
    Another option is to set an event handler of OnClick() instead of OnSubmit() for the delete buttons, and pass the name of the delete button pressed to your function. It will never enter your upload function in this manner, and can independantly do whatever delete functionality you want on a click event.
    HTH. Good luck.