Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
By cesarpo on
Hi,
For example, If i need to call a custom javascript function that is globally available, after a form ajax succes, what would be the best way to do it? The javascript code would be generated in the ajax submit callback.
Comments
http://www.jaypan.com/blog/ca
edit: sorry, wrong version of drupal.
thank you anyway! anyone
thank you anyway!
anyone else?
I'm looking for the same, I
I'm looking for the same, I need to add a js file after ajax_deliver() returns the page.
I was able to accomplish what
I was able to accomplish what I needed with ajax_command_invoke.
http://api.drupal.org/api/drupal/includes--ajax.inc/function/ajax_comman...
Would you please be so kind
Would you please be so kind and tell me how you did it?
In the end i coded the ajax by my own, but would like to know the 'Drupal way' :)
Looking for the
Looking for the drupal-solution too
My solution
I made a custom jquery function to call my function. If there is a better solution, feel free to correct.
In my callback function:
In a custom javascript, I created a jquery function
This works for me
Just wanted to add a quick note here that this solution worked great for me. Thanks for posting your code!
For my use, I wanted to call a custom javascript command and then do the normal behavior of inserting a rebuilt form element into ajax['#wrapper']. I did this using something similar to the following code in my callback function:
If anyone has any feedback on this approach, it would be great to hear.
This solution helped me out
This solution helped me out too! I wanted to use jQuery to modify field values, because I wasn't actually rebuilding a form. So I accomplished it like this, without using the .js file and a myJavascriptFunction:
Works great! But now what I really want to do is run other Javascript commands without using the .js file, ie. something like:
Does anyone have any insight as to how I can run commands like Window.alert() or Drupal.ckeditorOff() using an Ajax command array?
You're going to have to do
You're going to have to do what the solution above indicates; define a jQuery plugin function that executes whatever other JavaScript you want to execute and invoke it using ajax_command_invoke(). Here's a tutorial on the subject I found helpful: http://addictedtonew.com/archives/414/creating-a-jquery-plugin-from-scra...
The link is broken :(
The link is broken :(
This article helped me: Drupal 7 execute javascript code after a ajax call
You need create a custom ajax command
Here is the link for you
I have created it.
https://www.drupal.org/forum/support/module-development-and-code-questions/2018-12-18/creating-custom-ajax-command-in
an alternative
As an alternative, you can insert a script tag into the document.
$commands[] = ajax_command_append('body', '<script>alert("foo");</script>');
YMMV. I wanted to do it this way because I couldn't rely on my jQuery function definition being loaded after the function that I needed to call. Just thought I'd toss this out there for anyone else looking for another way.
Works for me
Great answer, thanks for posting...
.ajaxComplete() works for me also in 7
The solution suggested for Drupal 6 (Calling a function after an AHAH event in Drupal 6) works for me also fine in 7 and is quite straightforward. I've added the javascript inline:
Ajax complete
For me, .ajaxcomplete fired even when my form validation wasn't successful. The best way around this was to write a custom ajax plugin.
I included something like this in my active .js file:
You can get a good look at the structure of your args with console.log(), then drill down so your code only executes exactly when you need it to. The ajax plugins are called in ajax.js, if you want to take a look.
It will work, but actually
It will work, but actually I've since written a tutorial on how to do it in D7 now using ajax commands (which is the way to do it in D7). Calling a function after an #AJAX event (Drupal 7)
This is very nice tutorial.
This is very nice tutorial. Saved the day. :)
Thank You,
Shashwat Purav
This simple code...
I have been searching for this function for a very long time... Thank you!
$( document
$( document ).ajaxStop(function() {
// call your js code
});
Perform Action After Drupal Ajax
This will work, but it adds
This will work, but it adds extra overhead. Using the ajax_command_() series of functions provided by Drupal requires less scripting, and will have less overhead, as it is not called on every ajax command like the above code is.
Bullseyes!
In my case I'm working with the IEF module and I needed to execute an JS function (as the answer) with updated settings passed back by a submit function (without hacking IEF module or process) and occurs when you make a CTA to a form or link by Ajax you could use drupal_add_js function and sent anything you may need back to the browser as par of the global response object with no need to overwrite your previous Ajax callback.
For me perfect and fastest
For me perfect and fastest solution
It's an easy solution, but
It's an easy solution, but note that the code will be executed after every single AJAX request on the site - which can potentially cause a lot of overhead.
wish I could vote this up !
wish I could vote this up !
Leverage Drupal's behavior re-attachment
Late post I know, but in case it helps someone:
I was looking for something less global than
ajaxStop
orajaxComplete
(multiple panels on a checkout ajaxing at different times. So I used a little workaround leveraging Drupal's behaviour re-attachment. I wanted to add a spinner on very specific items on the page and remove them when specific ajax callbacks were complete so:In my JS file, I attached a behavior, with a
once
to the html element being returned/ replaced by the specific ajax command that I want to target. In this way, your code will always run when that ajax call has stopped. More detail:Hope it helps someone