0

I want to put a call to action in the middle of my content no matter what size the content it.

I know I will need some JavaScript in order to calculate the word count and the mid point. And I know I will need to take it a step further as I won't want to place the call to action inbetween a heading and a paragraph for example, so it will need some logic to work out the natural position. I think I probably also need some logic to work out the length and add it to the end if the content is particularly short.

Does anyone know where I can find that type of JavaScript code (preferably vanilla).

I'm guessing a need a content plugin. Does anyone know what type of content plugin I need to use?

  • Just to confirm, you mean that you need to insert the call to action half way down an article, rather than it appearing in the middle of the user's device screen? So that if the user was using a mobile, it could be that the call to action wasn't visible at the start, and only appeared when they scrolled down to that part of the article; is that right? And is it just articles that need to have this call to action added? – Robbie Jackson Sep 21 at 14:51
1

You need to make a decision: either you do this server-side or client-side. Server side requires a plugin written in php, while the client side a script written in Javascript. I would only use the client side if pixel-perfect alignment to other parts of the page is required.

Should you choose to write the plugin, it is very easy, write a Content plugin which implements the onAfterRender function; therein you would gather the body of the content with JFactory::getApplication()->getBody(), modify at will (i.e. load the module position and inject it) and apply the changes with setBody().

Should you want to go with a script, you have jQuery loaded to make this kind of things faster.

If the call to action is different for different users, you may run into cache issues with the plugin approach if you use full page caching.

| improve this answer | |
  • Wow amazing answer, I will give it a try. – Eoin Sep 22 at 9:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.