Skip to Content Skip to Menu

Assign a CB Auto Action to one specific page?

1 day 9 hours ago #343904 by BobBriscoe
How do I write inline javascript early in the head of one specific page.
I assume I should use a CB Auto Action of:
  • type: code
  • trigger: onBeforeCompileHead
  • Method: javascript(inline). 
But there's no way (that I can see) to assign such an action to one specific page? 

Hacked workround: I have used the above approach, but wrapped my code in an 'if' that matches the path of the specific page. But this surely can't be the proper approach, because it adds the code to every page, including backend pages.
 

Please Log in or Create an account to join the conversation.

  • krileon
  • krileon
  • ONLINE
  • Posts: 50418
  • Thanks: 8624
  • Karma: 1472
1 day 9 hours ago #343905 by krileon
Replied by krileon on topic Assign a CB Auto Action to one specific page?
I'd suggest just using CB Content Module instead. You can then assign it to your menu item. CB Content Module can output JavaScript, jQuery, and CSS in addition to regular content.

If there isn't a menu item to assign to or you still want to use onBeforeCompileHead then you'd need to add a condition to check for the URL. You can substitute in URL parameters using [get_PARAMETER] as well for easy access to parameter checks. PHP conditions can also be used to use Joomla API to retrieve the current URL and do checks against it however you need.


Kyle (Krileon)
Community Builder Team Member
Before posting on forums: Read FAQ thoroughly + Read our Documentation + Search the forums
CB links: Documentation - Localization - CB Quickstart - CB Paid Subscriptions - Add-Ons - Forge
--
If you are a Professional, Developer, or CB Paid Subscriptions subscriber and have a support issue please always post in your respective support forums for best results!
--
If I've missed your support post with a delay of 3 days or greater and are a Professional, Developer, or CBSubs subscriber please send me a private message with your thread and will reply when possible!
--
Please note I am available Monday - Friday from 8:00 AM CST to 4:00 PM CST. I am away on weekends (Saturday and Sunday) and if I've missed your post on or before a weekend after business hours please wait for the next following business day (Monday) and will get to your issue as soon as possible, thank you.
--
My role here is to provide guidance and assistance. I cannot provide custom code for each custom requirement. Please do not inquire me about custom development.

Please Log in or Create an account to join the conversation.

1 day 8 hours ago #343907 by BobBriscoe
Replied by BobBriscoe on topic Assign a CB Auto Action to one specific page?
More haste, less speed. Please read the question more carefully.

...early in the head

I have plenty of ways to add javascript to the body. However, unless it's in the head, users see the original page, then a flash to a new page (because the script does a redirect after encoding an extra attribute in the URL).

I ... wrapped my code in an 'if' ... But this surely can't be the proper approach

So, telling me "you'd need to add a condition" is what I've already done, and the question was asking how it should be done without a condition.

If it can't be done with CB, pls just say.

Please Log in or Create an account to join the conversation.

  • krileon
  • krileon
  • ONLINE
  • Posts: 50418
  • Thanks: 8624
  • Karma: 1472
1 day 6 hours ago #343909 by krileon
Replied by krileon on topic Assign a CB Auto Action to one specific page?
CB Content Module inserts into the head. It doesn't insert into the body unless you add that as inline inside the Content parameter.

 

Those all insert into the head.

So, telling me "you'd need to add a condition" is what I've already done, and the question was asking how it should be done without a condition.

That Joomla event isn't page specific. It fires on every page every time the head is compiled. You have to use a condition. Conditions in CB Auto Actions will stop the execution of the auto action. So your condition would prevent the JavaScript from being output entirely. No need for an IF statement in the JavaScript.

If it can't be done with CB, pls just say.

It can. As I said use a condition to check what URL you're on.

We even provide a substitution for getting the current URL to make this even easier. Specifically the following.

[cb:url location="current" /]

You can then use a REGEXP operator or CONTAINS operator to check against part of or the entirety of the currently being accessed URL. Note that substitution includes the full URL, which includes the domain name as well. It should basically just be the exact same URL as seen in the address bar. An example condition as follows.

Field: Custom > Value
Custom Value: [cb:url location="current" /]
Operator: Does Contain
Value: option=com_content&view=featured

This condition would match if my URL contains that fragment, which in my case is just the featured articles. Please be sure to take into account SEO here as again you'll want to supply the URL as seen in the address bar.


Kyle (Krileon)
Community Builder Team Member
Before posting on forums: Read FAQ thoroughly + Read our Documentation + Search the forums
CB links: Documentation - Localization - CB Quickstart - CB Paid Subscriptions - Add-Ons - Forge
--
If you are a Professional, Developer, or CB Paid Subscriptions subscriber and have a support issue please always post in your respective support forums for best results!
--
If I've missed your support post with a delay of 3 days or greater and are a Professional, Developer, or CBSubs subscriber please send me a private message with your thread and will reply when possible!
--
Please note I am available Monday - Friday from 8:00 AM CST to 4:00 PM CST. I am away on weekends (Saturday and Sunday) and if I've missed your post on or before a weekend after business hours please wait for the next following business day (Monday) and will get to your issue as soon as possible, thank you.
--
My role here is to provide guidance and assistance. I cannot provide custom code for each custom requirement. Please do not inquire me about custom development.

Please Log in or Create an account to join the conversation.

7 hours 46 minutes ago #343916 by BobBriscoe
Replied by BobBriscoe on topic Assign a CB Auto Action to one specific page?
I've unpublished my CB Auto Action. However, with CB Content Module the code no longer injects into the head of the relevant page (I've checked the page source). So I must have configured something wrongly. Here's how I configured it:
Joomla > Content > Site Modules >
  • √ Enable CB Content
Joomla > Content > Site Modules > CB Content > 
    [Module] tab >
  • Javascript: pasted js code (without the redundant path-matching condition)
  • Position: ::none:: (Default) (you say js is inherently injected into the head, and I couldn't see any position relevant to the head)
  • Published: Y (Default)
  • Access: Public
    [Menu assignment] tab >
  • selected the relevant menu (which is hidden on the frontend)
Joomla > System > Global Config:
  • I have editor set to None
  • I, as superuser, have no text filtering.

_________
Sorry, for doubting you before, but you have to admit that the CB terminology here is counter-intuitive:
  • a condition based on a 'Field' sounds like it's only to do with form fields (which implied to me that the body would already have had to load)
  • the term 'Content Module' sounds like it's to do with media content - the term 'content' is occasionally used for a script, but only one that customizes content (not the case for script in general).
From what I can find in the documentation about the CB Auto Action > Conditions > Custom field, it can only match against whole URLs, not solely the pathname part of the URL. So I would have to write a complex regex to avoid a false positive match, given arbitrary query strings or fragments in the URL or a backend page of similar name.

The javascript the regex would have had to emulate was:
    const path = window.location.pathname;
    if (!path.includes('/administrator/') && path.endsWith('/booking-menu')) { ... }



 

Please Log in or Create an account to join the conversation.

  • krileon
  • krileon
  • ONLINE
  • Posts: 50418
  • Thanks: 8624
  • Karma: 1472
6 hours 17 minutes ago #343917 by krileon
Replied by krileon on topic Assign a CB Auto Action to one specific page?

Position: ::none:: (Default) (you say js is inherently injected into the head, and I couldn't see any position relevant to the head)

The module has to be in a position. Joomla does not run modules in "none" position. Place it in a footer position on your page for example. It won't output any content to that footer (unless you add text/HTML to the Content parameter), but it'll let Joomla run the module. You can also go a step further here if you like and under Advanced Options set "Module Style" to "none", but shouldn't be necessary. That's just how modules work in Joomla so while seams a bit silly is required for the module to be ran by Joomla. We're actually using this method ourselves to customize some chunks of our own site and works pretty reliably.

Sorry, for doubting you before, but you have to admit that the CB terminology here is counter-intuitive:
a condition based on a 'Field' sounds like it's only to do with form fields (which implied to me that the body would already have had to load)

Conditions originally only supported fields. So the terminology was correct at the time. Since then custom condition types have been added and the terminology was left untouched since all our existing customers are already used to the UX. It probably does need to be renamed, but not sure what to rename it to at this point. Will have to think on that for future major releases for a better name.

the term 'Content Module' sounds like it's to do with media content - the term 'content' is occasionally used for a script, but only one that customizes content (not the case for script in general).

Joomla refers to Content as Articles or basically and text/html. This however is same situation as Conditions. Originally the module only output just text/HTML with substitution support. It has since expanded to support much more than that. Renaming an extension is not an easy task as it completely destroys all existing branding so the name was left as is. I don't really see what else to call it here regardless. I guess we could split it up into like 6 different modules, but I'd imagine that'd just annoy everyone and create a backwards compatibility problem then requiring a migration. Something we can consider for CB 3.0 maybe as these multi-featured modules I agree can be confusing (CB GroupJive Module is another one with a ton of modes).

From what I can find in the documentation about the CB Auto Action > Conditions > Custom field, it can only match against whole URLs, not solely the pathname part of the URL. So I would have to write a complex regex to avoid a false positive match, given arbitrary query strings or fragments in the URL or a backend page of similar name.

You can match parts of a URL as I've done in my example above. The "Does Contain" and "Does Not Contain" operators can do partial string matches. "Begins With" and "Ends With" conditions are also available and they do the same thing except partial start/end matching. To replicate your JavaScript condition you'd use the below.

Condition 1
Field: Custom > Value
Custom Value: [cb:url location="current" /]
Operator: Does Not Contain
Value: /administrator/
Condition 2
Field: Custom > Value
Custom Value: [cb:url location="current" /]
Operator: Ends With
Value: /booking-menu

You can have as many conditions as you want so feel free to add as many checks as needed to match whatever parts you need to match.


Kyle (Krileon)
Community Builder Team Member
Before posting on forums: Read FAQ thoroughly + Read our Documentation + Search the forums
CB links: Documentation - Localization - CB Quickstart - CB Paid Subscriptions - Add-Ons - Forge
--
If you are a Professional, Developer, or CB Paid Subscriptions subscriber and have a support issue please always post in your respective support forums for best results!
--
If I've missed your support post with a delay of 3 days or greater and are a Professional, Developer, or CBSubs subscriber please send me a private message with your thread and will reply when possible!
--
Please note I am available Monday - Friday from 8:00 AM CST to 4:00 PM CST. I am away on weekends (Saturday and Sunday) and if I've missed your post on or before a weekend after business hours please wait for the next following business day (Monday) and will get to your issue as soon as possible, thank you.
--
My role here is to provide guidance and assistance. I cannot provide custom code for each custom requirement. Please do not inquire me about custom development.

Please Log in or Create an account to join the conversation.

Moderators: beatnantkrileon
Powered by Kunena Forum