I want to run javascript with rules so I can do some client side things.

Comments

fago’s picture

Component: Rules Core » Rules Engine

Hm, we could add an "Add javascript" action, that just relies on drupal_add_js().

burningdog’s picture

That sounds good - I'd love this feature :) There's an interesting problem that would have to be solved in getting this right, though. Since rules are executed when the page is saved, adding drupal_add_js isn't going to do anything, since it's got to be added only once the rule has processed and drupal is bootstrapping again.

So there'd have to be something in Rules which picks up that javascript must be added, but only adds it via drupal_add_js when the new page (or whatever) is loading. Do I have that right?

mitchell’s picture

Title: Javascript Integration » Action: Add javascript
Version: 6.x-1.x-dev » 7.x-2.x-dev
Status: Active » Postponed
rv0’s picture

I stumbled upon this feature request

Any chance this would be possible to do in a "Custom PHP code" action?

dwangba’s picture

I am trying to get JavaScript code executed when a user clicks on a rules link.
I think Roger is right in #2 because the JavaScript won't execute using drupal_add_js in a custom rules action.
Can anyone point me in the right direction to achieve this. Thanks!

dwangba’s picture

I found a solution using behaviors.

Anonymous’s picture

Hello. Has this issue been followed up? I would find it very very helpful to be able to execute some client side javascript using the rules module. jakob

SilviaT’s picture

Same request for me, it would be nice to have it as an action

mitchell’s picture

Status: Postponed » Active

#2 & #5: No, that's not right. 'Drupal is initializing' would definitely work but is expensive, and 'Content is viewed' would probably work because Rules Forms Support can modify the served html, afair. I haven't looked at the code personally, but if fago suggests drupal_add_js() support will make this work, I'm gonna believe that.
#6: What did you do?
#m: The maintainers are waiting on a patch from you.

nocean’s picture

Not at all to take away from adding this feature, but I wanted to share how I managed to do this without a new action by using the drupal_set_message(); function and a dollop of CSS.

I wanted to fire off a bit of javascript when a node was created/edited. So, I simply setup my Event and Conditions, and then for Action I used "Execute Custom PHP Code" with the following PHP:

drupal_set_message('<script type="text/javascript">MY JAVASCRIPT CODE</script>','js-message');

That prints the script in a message box after the node is saved/edited and thus fires the JS. The one problem was it generated a message box, which was a bit unsightly. This is where the "js-message" comes in. By defining the message type as "js-message" I can then target the message box in CSS by adding the following to my stylesheet:

.js-message {display:none;}

Anyway, maybe not a fully-integrated solution, but it got the job done. Wanted to share in case anyone else wandered here looking for a solution as I did.

nambio’s picture

Perfect implementation, thanks daobydesign.

I implemented it as a custom action (rather than the custom php code thing) which is the only improvement I can think of before we get into *actual* best practice.

mitchell’s picture

Title: Action: Add javascript » Action: Add inline javascript code
Component: Rules Engine » Rules Core

drupal_add_js ('inline'): Executes a piece of JavaScript code on the current page by placing the code directly in the page. -- The example seems to reflect the intentions here, so changing title for that, specifically.

#10 lead me to read about drupal_add_css too. The two functions seem to work hand in hand and it similarly supports inline code, so that'll be an interesting issue, too. Also, Rules Forms Support might come in handy for "pre-post-processing" form elements, as shown in drupal_add_css/7#comment-23333
---

There's a support request for this in #1678364: Is Rules module designed to work on the backend or can it be used to display UI elements, so I left it open, for when a solution is implemented, to document an example.

luksak’s picture

Would be really nice to have this especially in cases where contrib modules like commerce use rules hooks like this one:

http://api.drupalcommerce.org/api/Drupal%20Commerce/sites!all!modules!co...

giorgio79’s picture

Version: 7.x-2.x-dev » 8.x-3.x-dev
Issue summary: View changes

Maybe 8x?

giorgosk’s picture

Another way is outlined here
and it used php session variables to trigger html code in template
https://www.drupal.org/node/1243544#comment-4841992

anybody’s picture

I've created a sandbox module based on the idea in #10.

I think it's a general requirement to have a way (action) to execute JavaScript in Rules. It's helpful in several situations like for tracking tools and other stuff.

Please help me to check the security of the Proof-Of-Concept sandbox module and discuss it here: https://www.drupal.org/node/2418735
It would be great if a member of the Drupal Security Team could have a look at it.

The Sandbox Module can be found here: https://www.drupal.org/sandbox/anybody/2418721

Thanks a lot!