-1

I need to set an event handler whose job is to return the function in which the handler is being set, and not the anonymous triggerhandler itself. How can this be achieved? See the code below:

if(subFormToServer())
    doSuccessDance();
else
    doErrorStuff();

function subFormToServer(thaGoods) {
  $('<iframe name="subform-target" style="display: none;"></iframe>')
    .bind('load',  function(){ return 0; }) // how to I "pass" these return statements "up" to my function or "escape" them, so to speak, so that what they're returning isn't the load/error handler functions, but actually the subFormToServer function?
    .bind('error', function(){ return 1; })
    .appendTo('body');

 $('<form method="POST" '+
          'action="http://server/formHandler.php" '+
          'target="subform-target"> '+

        '<input name=inputName              value="'+encodeURIComponent(thaGoods)+'">'+

    '</form>')[0]
    .submit();
}
wwaawaw
  • 6,867
  • 9
  • 32
  • 42
  • 2
    Even after reading it twice, I wasn't able to understand your question. Please consider breaking down the code even more and possibly provide something like a jsFiddle. – Marcus Riemer Aug 19 '12 at 09:11
  • possible duplicate of [Why is this function returning "undefined"?](http://stackoverflow.com/questions/4143580/why-is-this-function-returning-undefined) – Dagg Nabbit Aug 19 '12 at 10:42
  • I thought that pastebins and JSFs were frowned upon here because the bin could go offline and make the question worthless? – wwaawaw Aug 19 '12 at 23:05

1 Answers1

4

That isn't possible. Consider this situation, if it were:

// Assume form takes 10 seconds to respond
var x = subFormToServer(...)

// form has neither loaded nor errored yet - what does x contain?
alert(x)

Your best bet is:

subFormToServer(thaGoods, doSuccessDance, doErrorStuff);

function subFormToServer(thaGoods, worked, didnt) {
    $('<iframe name="subform-target" style="display: none;"></iframe>')
        .on('load',  worked)
        .on('error', didnt)
        .appendTo('body');

    $('<form />', {
        method: "POST",
        action: "http://server/formHandler.php",
        target: "subform-target"
    }).append(
        $('<input />', {
            name: "inputName",
            value: encodeURIComponent(thaGoods)
        })
    ).submit()
}
wwaawaw
  • 6,867
  • 9
  • 32
  • 42
Eric
  • 95,302
  • 53
  • 242
  • 374