2021年3月7日星期日

Joomla: how to add script to header?

First thing first the page where I have the script:

https://www.glansbeton.be/uitvoering/ (the floating button with gratis offerte aanvragen)

this is my script code:

use Joomla\CMS\Factory;  $document =  Factory::getDocument();  // Add script  $Script = "      <script>;          jQuery(window).scroll(function(){              if (jQuery(this).scrollTop() > 70) {                  jQuery('.extensionbuy_now').addClass('btn-".$lftrght."');              } else {jQuery('.extensionbuy_now').removeClass('btn-".$lftrght."');              }          });      </script>";        if($btn_align == 0)  {      $CAlign .= '$(document).ready(function(){ $("#buttn").addClass("zero"); });';  }  else if($btn_align == 50)  {      $CAlign .= '$(document).ready(function(){ $("#buttn").addClass("fifty"); });';  }  else  {      $CAlign .= '$(document).ready(function(){ $("#buttn").addClass("hundred"); });';  }    if($btn_float == 0)  {        $CFloat .= '$(document).ready(function(){ $(".extensionbuy_now").addClass("btn_zero"); });';  }  else  {        $CFloat .= '$(document).ready(function(){ $(".extensionbuy_now").addClass("btn_hundred"); });';  }    $Script .= '<script type="text/javascript">';  $Script .= $CAlign;  $Script .= '</script>';  $Script .= '<script type="text/javascript">';  $Script .= $CFloat;  $Script .= '</script>';

If I simply echo the script: echo $Script;

The output is working like it has to be. So my script is good and working. Now I want to place the script outside the body.

$document->addStyleDeclaration($style);  

This is not working.
Did read a lot, but I don't know how to do this.
Can someone give me some advice?

https://stackoverflow.com/questions/66520533/joomla-how-to-add-script-to-header March 08, 2021 at 03:29AM

没有评论:

发表评论