Include external javascript in the 1st position

Everything to do with Joomla! 3.x templates and templating.

Moderator: General Support Moderators

Forum rules
Forum Rules
Absolute Beginner's Guide to Joomla! <-- please read before posting, this means YOU.
Forum Post Assistant - If you are serious about wanting help, you will use this tool to help you post.
Windows Defender SmartScreen Issues <-- please read this if using Windows 10.
Locked
EvanGR
Joomla! Enthusiast
Joomla! Enthusiast
Posts: 242
Joined: Fri Oct 10, 2008 5:30 pm
Location: Greece

Include external javascript in the 1st position

Post by EvanGR » Wed Sep 19, 2018 8:24 am

Hello,

I want to include javascript in the template (after the styles), but have it come absolutely first, before the rest of the scripts (because some depend on it).

Is this possible?

Thanks

User avatar
john-doe
Joomla! Ace
Joomla! Ace
Posts: 1008
Joined: Tue Apr 19, 2011 7:39 pm
Location: Colombia
Contact:

Re: Include external javascript in the 1st position

Post by john-doe » Thu Sep 20, 2018 9:51 pm

Depends. If you are doing it in a 100% Custom template from scratch you have to play with the order settings in the head of the index.php:

Code: Select all

<?php
defined( '_JEXEC' )or die;
$doc = JFactory::getDocument();
$this->language = $doc->language;
$this->direction = $doc->direction;
$headData = $doc->getHeadData();
$doc->setHeadData( $headData );

$doc->_script = array();

$doc->addScript( 'templates/' . $this->template . '/js/my-script.js' );

$doc->addStyleSheet( 'templates/' . $this->template . '/css/default.css' );


unset( $doc->_scripts[ JURI::root( true ) . '/media/jui/js/bootstrap.min.js' ] );

//unset( $doc->_scripts[ JURI::root( true ) . '/media/jui/js/jquery.min.js']);
//unset( $doc->_scripts[ JURI::root( true ) . '/media/jui/js/jquery-noconflict.js']);
//unset( $doc->_scripts[ JURI::root( true ) . '/media/jui/js/jquery-migrate.min.js']);

?>

But with this some JS loaded by default from Joomla Core you have to unset them if you need so.

But inevitably CSS comes above JS.

How does that external JS should work?
www.aldemar-hernandez.com - Custom templates and design services.

EvanGR
Joomla! Enthusiast
Joomla! Enthusiast
Posts: 242
Joined: Fri Oct 10, 2008 5:30 pm
Location: Greece

Re: Include external javascript in the 1st position

Post by EvanGR » Fri Sep 21, 2018 6:56 am

Hello, and thanks.

I am doing it in a custom template, which I am trying to optimize.

Specifically, a custom version of jquery which is (unfortunately) forced very early in the <head> tag, via a hardcoded script element, before joomla's head section.

Not optimal, scripts should come after styles, and jquery should come first before the rest of the scripts.

So I want to include that custom version of jquery after the styles, but first in joomla's scripts queue.

KianWilliam
Joomla! Guru
Joomla! Guru
Posts: 564
Joined: Thu Jan 12, 2017 10:13 am

Re: Include external javascript in the 1st position

Post by KianWilliam » Fri Sep 21, 2018 7:07 am

I have below questions:
a) In codes below you get head data: $headData = $doc->getHeadData() and then you set it immediately
$doc->setHeadData($headData); with no change. why?
b) you set $doc->_script = [] to array then in unset ($doc->_scripts[])
These two are the same array or different?
Kian William

User avatar
john-doe
Joomla! Ace
Joomla! Ace
Posts: 1008
Joined: Tue Apr 19, 2011 7:39 pm
Location: Colombia
Contact:

Re: Include external javascript in the 1st position

Post by john-doe » Mon Sep 24, 2018 4:07 pm

KianWilliam wrote:
Fri Sep 21, 2018 7:07 am
a) In codes below you get head data: $headData = $doc->getHeadData() and then you set it immediately
$doc->setHeadData($headData); with no change. why?
I don't recall the exact purpose of both statements, but as far as i remember it was a mixture of solutions I had when i was trying to load external JS. I wish I had a better answer.
KianWilliam wrote:
Fri Sep 21, 2018 7:07 am
b) you set $doc->_script = [] to array then in unset ($doc->_scripts[])
These two are the same array or different?
Kian William
Actually are on the same array (as far as i understand [i'm not good with arrays]).
I had to unset the bootstrap.min.js from the joomla core since it made a conflict with my site's bootstrap, since Joomla's is 2.3.2 and mine is 3.3.7; that was as well a solution i have found in this forum or in the magazine, i can't recall where.
www.aldemar-hernandez.com - Custom templates and design services.

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

Re: Include external javascript in the 1st position

Post by SharkyKZ » Mon Sep 24, 2018 4:20 pm

$_script and $_scripts are different things. $_script contains inline script. $_scripts contain script files. By setting $_script to an empty array you are removing inline script.

KianWilliam
Joomla! Guru
Joomla! Guru
Posts: 564
Joined: Thu Jan 12, 2017 10:13 am

Re: Include external javascript in the 1st position

Post by KianWilliam » Tue Sep 25, 2018 8:13 am

Alright, thanks for the response
Kian William


Locked

Return to “Templates for Joomla! 3.x”