简体   繁体   中英

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?

I'm not completely clear on where you're trying to implement this but see if the following helps:

<?php
   $document = JFactory::getDocument();
   $document->addScript('/media/system/js/sample.js'); // path to js script
?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM