Advertisement

Script in header AND at the end of index.php?

General questions relating to Joomla! 5.x.

Moderator: General Support Moderators

Forum rules
Forum Rules
Absolute Beginner's Guide to Joomla! <-- please read before posting.
Forum Post Assistant - If you are serious about wanting help, you should use this tool to help you post.
Windows Defender SmartScreen Issues <-- please read this if using Windows 10
Post Reply
SocketPup
Joomla! Enthusiast
Joomla! Enthusiast
Posts: 134
Joined: Thu Mar 14, 2024 7:00 am

Script in header AND at the end of index.php?

Post by SocketPup » Tue May 14, 2024 11:37 am

In my index.php, I use

Code: Select all

<jdoc:include type="scripts" />
to mark out where my script is supposed to be.
Is it possible to have more than one?
To be more specific, I need to have one script inside the <head> and another at the end of the document.

I use joomla.asset and $wa->useStyle('mystyle')->useScript('myscript'); to load the scripts into the document.

Advertisement
User avatar
Pavel-ww
Joomla! Ace
Joomla! Ace
Posts: 1713
Joined: Tue Jun 30, 2020 12:17 pm

Re: Script in header AND at the end of index.php?

Post by Pavel-ww » Tue May 14, 2024 12:10 pm

SocketPup wrote: Tue May 14, 2024 11:37 am I use joomla.asset and $wa->useStyle('mystyle')->useScript('myscript'); to load the scripts into the document.
Hi. Scripts/syles should be registered before use.
Exemple:

Code: Select all

$wa->registerStyle(...);
$wa->useStyle(...);

$wa->registerScript(...);
$wa->useScript(...);
Or in one line

Code: Select all

$wa->registerAndUseStyle(...);

$wa->registerAndUseScript(...);


SocketPup
Joomla! Enthusiast
Joomla! Enthusiast
Posts: 134
Joined: Thu Mar 14, 2024 7:00 am

Re: Script in header AND at the end of index.php?

Post by SocketPup » Wed May 15, 2024 7:02 am

Pavel-ww wrote: Tue May 14, 2024 12:10 pm
Hi. Scripts/syles should be registered before use.

Or in one line

Code: Select all

$wa->registerAndUseStyle(...);

$wa->registerAndUseScript(...);

This doesn't work, if I use the above command, my page ends up void of css (also, I use the code from Cassiopeia template and they don't seem to use that command anywhere).

Also, it doesn't answer my original question.

User avatar
Pavel-ww
Joomla! Ace
Joomla! Ace
Posts: 1713
Joined: Tue Jun 30, 2020 12:17 pm

Re: Script in header AND at the end of index.php?

Post by Pavel-ww » Wed May 15, 2024 8:26 am

SocketPup wrote: Wed May 15, 2024 7:02 am This doesn't work, if I use the above command, my page ends up void of css (also, I use the code from Cassiopeia template and they don't seem to use that command anywhere).

Also, it doesn't answer my original question.
Hi. I don't know why this does not work for you. Perhaps you are doing something wrong.

Let's look at the code from Cassiopea:

For WA to work, it must first be called.
1.jpg
--

Such commands are used in Cassiopea.
2.jpg
--

Here is an example of connecting custom scripts and styles via WA in one of my projects.
5.jpg
--


This line can only be the one in the document.
3.jpg
It include all scripts added through WA. You can move it to the end of Body. Then all scripts will be below, but this can cause problems, because for some scenarios, the presence of scripts in Head is critical.

If you want to leave the default scripts in Head and separately connect your custom scripts at the bottom of the Body, you do not need WA. Just do it using standard html
4.jpg
You do not have the required permissions to view the files attached to this post.

SocketPup
Joomla! Enthusiast
Joomla! Enthusiast
Posts: 134
Joined: Thu Mar 14, 2024 7:00 am

Re: Script in header AND at the end of index.php?

Post by SocketPup » Thu May 16, 2024 3:36 am

Pavel-ww wrote: Wed May 15, 2024 8:26 am
SocketPup wrote: Wed May 15, 2024 7:02 am This doesn't work, if I use the above command, my page ends up void of css (also, I use the code from Cassiopeia template and they don't seem to use that command anywhere).

Also, it doesn't answer my original question.
Hi. I don't know why this does not work for you. Perhaps you are doing something wrong.
This code works:

Code: Select all

$wa->useStyle('mystyle')
->registerAndUseScript('myscript');
BUT, if I use

Code: Select all

$wa->registerAndUseStyle('mystyle')
->registerAndUseScript('myscript');
My style will not be used in the project, for some reason.

SocketPup
Joomla! Enthusiast
Joomla! Enthusiast
Posts: 134
Joined: Thu Mar 14, 2024 7:00 am

Re: Script in header AND at the end of index.php?

Post by SocketPup » Thu May 16, 2024 3:38 am

Pavel-ww wrote: Wed May 15, 2024 8:26 am
If you want to leave the default scripts in Head and separately connect your custom scripts at the bottom of the Body, you do not need WA. Just do it using standard html
4.jpg
Oh ok, so it's fine to add some scripts the traditional way directly into index.php?
With Google Maps, it seems they want the main code inside the head-element, and the actual calling of the map (with key and all) at the end of document.

User avatar
Pavel-ww
Joomla! Ace
Joomla! Ace
Posts: 1713
Joined: Tue Jun 30, 2020 12:17 pm

Re: Script in header AND at the end of index.php?

Post by Pavel-ww » Thu May 16, 2024 7:16 am

SocketPup wrote: Thu May 16, 2024 3:38 am Oh ok, so it's fine to add some scripts the traditional way directly into index.php?
With Google Maps, it seems they want the main code inside the head-element, and the actual calling of the map (with key and all) at the end of document.
Hi.
If your goal is not to create a template install package for mass distribution and you only work on your own site, you do not need WA at all. Just create Cassiopea child template, copy files you need to edit (index.php and ect) from Cassiopea parent to Cassiopea child, add custom scripts and styles via traditional way directly in index.php in any place you want, in Head or Body.

Example
1.jpg
Result in output HTML
2.jpg
You do not have the required permissions to view the files attached to this post.

SocketPup
Joomla! Enthusiast
Joomla! Enthusiast
Posts: 134
Joined: Thu Mar 14, 2024 7:00 am

Re: Script in header AND at the end of index.php?

Post by SocketPup » Sun May 26, 2024 1:56 pm

OK, got it. Thanks.

SharkyKZ
Joomla! Virtuoso
Joomla! Virtuoso
Posts: 3074
Joined: Fri Jul 05, 2013 10:35 am
Location: Parts Unknown

Re: Script in header AND at the end of index.php?

Post by SharkyKZ » Sun May 26, 2024 5:52 pm

You should never add script tags manually in Joomla. This can cause issues with plugins, overrides and is not CSP friendly. If you use defer attribute, there should be no need to place scripts at the bottom anyways.

User avatar
Pavel-ww
Joomla! Ace
Joomla! Ace
Posts: 1713
Joined: Tue Jun 30, 2020 12:17 pm

Re: Script in header AND at the end of index.php?

Post by Pavel-ww » Tue May 28, 2024 7:13 am

SharkyKZ wrote: Sun May 26, 2024 5:52 pm You should never add script tags manually in Joomla. This can cause issues with plugins, overrides and is not CSP friendly. If you use defer attribute, there should be no need to place scripts at the bottom anyways.
Hi @SharkyKZ. There have never been problems with this, of course, if you understand what you are doing.

Advertisement

Post Reply

Return to “General Questions/New to Joomla! 5.x”