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
Include external javascript in the 1st position
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.
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.
-
- Joomla! Enthusiast
- Posts: 242
- Joined: Fri Oct 10, 2008 5:30 pm
- Location: Greece
- john-doe
- Joomla! Ace
- Posts: 1008
- Joined: Tue Apr 19, 2011 7:39 pm
- Location: Colombia
- Contact:
Re: Include external javascript in the 1st position
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:
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?
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 inevitably CSS comes above JS.
How does that external JS should work?
www.aldemar-hernandez.com - Custom templates and design services.
-
- Joomla! Enthusiast
- Posts: 242
- Joined: Fri Oct 10, 2008 5:30 pm
- Location: Greece
Re: Include external javascript in the 1st position
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.
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.
-
- Joomla! Guru
- Posts: 561
- Joined: Thu Jan 12, 2017 10:13 am
Re: Include external javascript in the 1st position
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
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
- john-doe
- Joomla! Ace
- Posts: 1008
- Joined: Tue Apr 19, 2011 7:39 pm
- Location: Colombia
- Contact:
Re: Include external javascript in the 1st position
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 ama) In codes below you get head data: $headData = $doc->getHeadData() and then you set it immediately
$doc->setHeadData($headData); with no change. why?
Actually are on the same array (as far as i understand [i'm not good with arrays]).KianWilliam wrote: ↑Fri Sep 21, 2018 7:07 amb) you set $doc->_script = [] to array then in unset ($doc->_scripts[])
These two are the same array or different?
Kian William
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.
-
- Joomla! Hero
- Posts: 2850
- Joined: Fri Jul 05, 2013 10:35 am
- Location: Parts Unknown
Re: Include external javascript in the 1st position
$_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.
-
- Joomla! Guru
- Posts: 561
- Joined: Thu Jan 12, 2017 10:13 am
Re: Include external javascript in the 1st position
Alright, thanks for the response
Kian William
Kian William