views:

51

answers:

4

I have a simple post, which executed the data returned as a script as such:

$.post("/home", { foo: 'bar' }, function(){
    //callback function
}, "script");

Here's what happens:

  1. The request gets sent
  2. A piece of JS code is returned as a response and executed
  3. The callback is executed

I need the callback to be executed before the returned JS is executed. Is there a way to achieve this?

Thanks!

A: 

This $.post() function does not automatically execute the JS which is returned. It is probably executed in the callback function.

Sjoerd
it does, if you specify `script` as the fourth parameter, which I did.
yuval
well then, i think you just answered your own question ;)
Patricia
@Sjoerd Yes it does.
meagar
A: 

Hello,

You could use "BeforeSend" option that is executed before send the ajax request:

$.ajax({

    type: "POST",

    url: "SOMEURL",

    data: { "WHATEVER": whatever },

    beforeSend: function() {

      //DO WHATEVER BEFORE SEND AJAX CALL

    }
.....

});

}

Hope this helps.

best regards.

Jose

Josemalive
I need it execute after the request is sent and before the script is executed. `beforeSend` gets executed before the request is sent. thanks though!
yuval
Helo Yuval,maybe this ajax event could help you ".ajaxStop event" check this page: http://api.jquery.com/ajaxStop/. Im not sure if the code into ajaxStop will be executed before your js returned by ajax call. But maybe could work.You could check .ajaxSucess too here: http://api.jquery.com/ajaxSuccess/Hope this helps.Regards.
Josemalive
+2  A: 

You can't change how JQuery handle the request. After all, a callback is a callback and it gets executed once the job is done! You can, however, set the dataType to "text" and then eval your Javascript.

Ex:

$.post("/home", { foo: 'bar' }, function(data){
   //callback function

   // execute JS
   $.globalEval( data );
}, "text");

** UPDATE **

This is what JQuery does internally (snipped) :

// If the type is "script", eval it in global context
} else if ( type === "script" || !type && ct.indexOf("javascript") >= 0 ) {
   jQuery.globalEval( data );
}

So, there is no security risk with taking the globalEval out of the Ajax request and executing it in the callback function.

Yanick Rochon
+1 for simple and ideal solution
meagar
this seems ideal. does jquery execute the script in the same way? (i.e. call `eval()`)? If not, are there any security flaws in doing that?
yuval
This is what JQuery does internally if you set dataType as "script", so no. If there is no risk one way, there is no risk another. :) Read the update for the answer.
Yanick Rochon
I looked at the jQuery source code and that's not how it does it at all - it created a script tag, handles the script loading, various IE memory leaks, and then appends the script tag to the head element. I think I'll just find a workaround - `eval` doesn't seem to do the same job. thanks anyways!
yuval
it creates a <script> tag only if the requested URL is using GET and if the dataType is "script". This is not your case here.
Yanick Rochon
+1  A: 

Use $.ajax() and build your own script tag.

$.ajax({url:"/home",
        data: { foo: 'bar' },
        type: "POST",
        dataType: 'text',     // Return dataType is "text" instead of "script"
        success: function( script ){
           // run your code
           alert('mycode');
           // then create your own script tag
           var tag = document.createElement('script');
           tag.setAttribute('type','text/javascript');
           tag.appendChild(document.createTextNode(script));
           document.getElementsByTagName('head')[0].appendChild(tag);
        }
});
patrick dw