? ? Success

User tests: Successful: Unsuccessful:

avatar hans2103
hans2103
4 May 2016

Reason of Change

I would like to be able to load my Javascript just before </body>.

Summary of Changes

Added a new class JDocumentRendererHtmlFooter in /libraries/joomla/document/html/renderer/footer.php and added function addFooterScript in /libraries/joomla/document/document.php so you can add scripts using JFactory::getDocument()->addFooterScript('JavaScript_File_Path.js') and render it using <jdoc:include type="footer" />.

Testing Instructions

In index.php in you template folder add just before the tag </body>

<jdoc:include type="footer" />

Now in layouts inside your template's folder you can use the following code to add javascript files to footer:

$doc = JFactory::getDocument();
$doc->addFooterScript('JavaScript_File_Path.js');

Refresh your website and notice that the script is placed just before </body>

Reasons why

The async and defer attributes for the <script> element have great support to pause and defer the execution of the script. But still... they are downloaded in the head.

Clear explain of async and defer can be found on: http://www.growingwiththeweb.com/2014/02/async-vs-defer-attributes.html

Comment by Paul Irish in Modernizr github repo why not to load modernizr in the head... and therefor move scripts to the end of body.
Modernizr/Modernizr#878 (comment)

  • If you need html5shiv, split it out from modernizr and conditionally include it for IE8 (better as a conditionally included inline script)
  • Determine if your use of Modernizr requires it to run in the <head>. If it doesn't, defer loading it till the bottom of <body>
  • If you do need it in the head, make the build as small as possible and drop it into an inline script.
  • Do not continue to use a <script src=modernizr.js> from within the <head>

Thanks to @rishiv3 for his small repo: https://github.com/rishiv3/joomla-adding-javascript-file-in-the-footer

@dgt41 can you please look at this PR too?

avatar hans2103 hans2103 - open - 4 May 2016
avatar hans2103 hans2103 - change - 4 May 2016
Status New Pending
avatar joomla-cms-bot joomla-cms-bot - change - 4 May 2016
Labels Added: ?
avatar hans2103 hans2103 - change - 4 May 2016
The description was changed
avatar brianteeman brianteeman - change - 4 May 2016
Category Feature Request JavaScript Libraries
avatar brianteeman brianteeman - change - 4 May 2016
Labels Added: ?
avatar allrude
allrude - comment - 4 May 2016

Tested succesfully, thanks Hans2103 for a great addition

avatar ghazal ghazal - test_item - 4 May 2016 - Tested successfully
avatar ghazal
ghazal - comment - 4 May 2016

I have tested this item :white_check_mark: successfully on 6e298ab


This comment was created with the J!Tracker Application at issues.joomla.org/joomla-cms/10223.

avatar allrude allrude - test_item - 4 May 2016 - Tested successfully
avatar allrude
allrude - comment - 4 May 2016

I have tested this item :white_check_mark: successfully on 6e298ab

Tested succesfully, thanks Hans2103 for a great addition


This comment was created with the J!Tracker Application at issues.joomla.org/joomla-cms/10223.

avatar Fedik
Fedik - comment - 4 May 2016

déjà-vu :smile:

avatar brianteeman
brianteeman - comment - 4 May 2016

@fedir I wasnt going to ask if you had created a similar PR in the past

On 4 May 2016 at 13:07, Fedir Zinchuk notifications@github.com wrote:

déjà-vu [image: :smile:]


You are receiving this because you are subscribed to this thread.
Reply to this email directly or view it on GitHub
#10223 (comment)

Brian Teeman
Co-founder Joomla! and OpenSourceMatters Inc.
http://brian.teeman.net/

avatar hans2103
hans2103 - comment - 4 May 2016

Why wasn't it implemented before?
And what are the changes this one will?

avatar brianteeman
brianteeman - comment - 4 May 2016

@fedik which was your pr

avatar mbabker
mbabker - comment - 4 May 2016

Unfortunately it still isn't just as simple as this. You still should deal with at least duplicates now, and integration with JHtml::script() (otherwise you possibly have people trying to load jQuery in both the head through the JHtml function and the body through direct including of that file and nothing using JHtml::script() can make use of this right now meaning everything included into the body has no override possibility). IMO that's why things like this keep stalling out.

avatar Fedik
Fedik - comment - 4 May 2016

I just meant that it is old topic #2730 #2688 #8271 and in google group also was something.
And for my opinion: For now it will brings more problem than actually fix.

avatar brianteeman
brianteeman - comment - 4 May 2016

Thanks for providing the references @fedik

avatar hans2103
hans2103 - comment - 4 May 2016

oh boy oh boy...

By reading the mentioned old topics I think it might be better to rewrite the entire addScript function... in J4. No BC needed. :-)
With the rewrite of the addSript function you can even add an order (first, second, third.... last) variable.

or...

We could improve this PR with full backwards compatibility.

avatar brianteeman
brianteeman - comment - 4 May 2016

Now you have your answer why previous PR have not been merged :)

avatar hans2103
hans2103 - comment - 4 May 2016

yeah!
back to the drawing board I guess... or something to tackle during #JAB16.

avatar andrepereiradasilva
andrepereiradasilva - comment - 4 May 2016

yeah javascript and css rendering in JDocument is, IMHO, full of issues .

Just to name a few:

  • doesn't process js/css dependencies
  • inline scripts are merged which IMHO, does not make sense because the js order is important.
  • inline css are merged which IMHO, does not make sense because the css order is important.
  • doesn't allow to select the HTML doc position (footer/body) where they should be rendered
  • doesn't allow data atrributes
  • exist multiple methods with different behaviours (JHtml, addScript, addScriptVersion, addScriptDeclaration, etc)
  • don't allow IE conditional statements
  • etc

This needs a full rework IMO and there are several PR proposing most of this changes needed.
But i guess it will be very difficult before 4.0 because of B/C

avatar brianteeman brianteeman - change - 5 May 2016
Status Pending Closed
Closed_Date 0000-00-00 00:00:00 2016-05-05 08:00:38
Closed_By brianteeman
avatar brianteeman brianteeman - close - 5 May 2016
avatar brianteeman brianteeman - close - 5 May 2016
avatar brianteeman
brianteeman - comment - 5 May 2016

I am closing this PR and have created a new placeholder issue #10250 where anyone interested in moving this concept forward can discuss together possible approaches. Thanks


This comment was created with the J!Tracker Application at issues.joomla.org/joomla-cms/10223.

Add a Comment

Login with GitHub to post a comment