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.
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:
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:
// inside Ajax callback function$commands=array();// set Title and body form elements using jQuery's .val().$commands[]=ajax_command_invoke('#edit-body-title','val',array('new Title'));$commands[]=ajax_command_invoke('#edit-body-und-0-value','val',array('New body Text'));// Use ajax_command_html to replace the contents of with an empty string$commands[]=ajax_command_html('#div-ajax-form','');// Hide contents of ajax formreturnarray('#type'=>'ajax','#commands'=>$commands);
Works great! But now what I really want to do is run other Javascript commands without using the .js file, ie. something like:// This doesn't work$commands[]=ajax_command_invoke('Window','alert',array('Hi there!'));// Nor does this, which is really what I'd like to accomplish without needing to wrap it in a .js file function$commands[]=ajax_command_invoke(NULL,'Drupal.ckeditorOff',array('edit-body')
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 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...
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.
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:
$(function(){if(!Drupal.Ajax)return;
Drupal.Ajax.plugins.yourAction =function(hook, args){if(hook =='message'&& args.data.form_id =='your_form'&& args.data.status ==true){// args.data.status is true only after validation clears// Your js here}};});
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.
(function($){
Drupal.testAjax ={// Our form
form_id: Your-Form-ID' //Yes, I tested on my extended node creation form};
Drupal.behaviors.testAjax ={
attach:function(context, settings){// We extend Drupal.ajax objects for all AJAX elements in our form for(ajax_el in settings.ajax){if(typeof Drupal.ajax[ajax_el]!='undefined'&& Drupal.ajax[ajax_el].element.form){if(Drupal.ajax[ajax_el].element.form.id === Drupal.testAjax.form_id){
Drupal.ajax[ajax_el].beforeSend = Drupal.testAjax.beforeSend;
Drupal.ajax[ajax_el].success = Drupal.testAjax.success;
Drupal.ajax[ajax_el].error = Drupal.testAjax.error;}}}}};// Disable form
Drupal.testAjax.beforeSend =function(first, second){
console.log("Before Submit");// Call Drupal parent method
Drupal.ajax.prototype.beforeSend.call(this, first, second);}
Drupal.testAjax.success =function(response, status){
console.log("On Success");// Call original method with main functionality
Drupal.ajax.prototype.success.call(this, response, status);}
Drupal.testAjax.error =function(response, uri){
console.log("Error");
Drupal.testAjax.enableForm(this.element.form);// Call original method with main functionality
Drupal.ajax.prototype.error.call(this, response, uri);}})(jQuery);
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.
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.
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.
Comments
http://www.jaypan.com/blog/ca
edit: sorry, wrong version of drupal.
The Drupal organization has shut down discussion on improvement of the forums:https://www.drupal.org/node/2536122
It's time to start a new forum somewhere else. The Drupal organization does not care about the forums.
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:
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...
------------------------------------------
Read more: http://ryanszrama.com/topics/drupal
The link is broken :(
The link is broken :(
This article helped me: Drupal 7 execute javascript code after a ajax call
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)
The Drupal organization has shut down discussion on improvement of the forums:https://www.drupal.org/node/2536122
It's time to start a new forum somewhere else. The Drupal organization does not care about the forums.
This is very nice tutorial.
This is very nice tutorial. Saved the day. :)
Skype ID: shashwatpurav8
$( 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.
The Drupal organization has shut down discussion on improvement of the forums: https://www.drupal.org/node/2536122
It's time to start a new forum somewhere else. The Drupal organization does not care about the forums.
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.
The Drupal organization has shut down discussion on improvement of the forums:https://www.drupal.org/node/2536122
It's time to start a new forum somewhere else. The Drupal organization does not care about the forums.
来自 https://www.drupal.org/node/1028410