tags:

views:

102

answers:

5

This is my first experience with javascript, and... Well... Ugh. Here's what's happening:

function step_1(id) {
    //blah blah
    step_2(id);
}
function step_2(id) {
    //blah blah
    step_3(id);
}
function step_3(id) {
    //blah blah
    alert(id);
}

step_1(0); // I can stick any number here, same thing happens...

The alert pops up and says "Undefined". But, if I throw an alert(id); in step_2, then both alerts say "0".

Why/how is id undefined? What am I doing wrong?

I've even tried reassigning id in each function, like:

var nid = id;
step_2(nid);

etc... But that still doesn't work without the alerts.

EDIT: Since my example apparently works fine, perhaps it would help to look at the blah blah that's going on in my code. It works fine, unless I take out the alert(id); on line 11.

+5  A: 

There's a difference between step_2 and step2. And all your other little steps ...

Robusto
Ack, that was just me typing the question too quickly. I fixed that. If only the problem was that simple.
Keene Maverick
Well, I just took your new code and ran it in Firebug verbatim, and I get an alert with "0" as its message. So I don't know what your problem is. Maybe there's some error in the //blah blah part?
Robusto
I've been pouring over this code for 3 days now, I can't find it. I just added a pastebin link to my code.
Keene Maverick
A: 

_in the steps may be killing program and your buzz step2 and step_2 are two different things

Anthony
+4  A: 

You have a line (line 30) at the end of checkUpload that calls itself without any parameters:

    window.setTimeout('checkUpload();', 333);

It seems like this is what you meant to do:

    window.setTimeout(function() { checkUpload(id); }, 333);

    // which is the equivalent to:
    // window.setTimeout("checkUpload(" + id + ");", 333);
Jeff Meatball Yang
YES!! Haha, you're awesome! I feel like a total idiot now. :D
Keene Maverick
Added a "harshing-my-buzz" tag.
Jeff Meatball Yang
So show the meatball some love and give him the checkmark. =D
Robusto
+2  A: 

Your script re-calls checkUpload() via a timer without passing along the id parameter it's expecting.

Line 30

window.setTimeout('checkUpload();', 333);

Change to

window.setTimeout( function(){ checkUpload(id); }, 333 );

In the future, as a helpful piece of advice, I encourage you to post your actual problem the first time around, and not a pared down example that you think illustrates the issue. Just saves everyone time and effort ;)

Peter Bailey
Actually, it's even better if you just make sure that your pared-down example actually illustrates the problem!
Gabe
A: 

Mate, I don't know if this answer is relevant as pointed by others, but couple of observations from seeing your code.

if (uploadFrame.contentDocument.readyState == 'complete') {
            if (uploadFrame.contentDocument.getElementById('new_image_id')) {
                    var new_id = uploadFrame.contentDocument.getElementById('new_image_id').innerHTML;

Really! you should know that '==' is not the correct operator but '===' is. A common mistake made by js learners/users. Also you may want to recheck the id, is it 'new_image_id' or 'new_image_'+id.

questzen
Is there any situation in which readyState will return a non-string that =='s true to 'complete' ? Or is the javascript === different from php's === ? Or do I misunderstand the ==/=== concept entirely?And yes, I want 'new_image_id' - that's the mysql_insert_id() for the just-uploaded image. Let's me know where to send the image description in the AJAX request.
Keene Maverick
== typically return value equality, not type equality. The tautology of js is a bitch, the thumbrules being use only === and !== as that would be your intent. A reference from web: http://www.webreference.com/js/column26/stricteq.html
questzen