?
avatar kossa
kossa
16 Dec 2014

Steps to reproduce the issue

if you add a js file using :
JFactory::getDocument()->addScriptDeclaration('
// some code
');
OR
$document = JFactory::getDocument();
$document->addScript('PATH_TO_SCIPRT');
will be automatically added in the head of the document

Expected result

In the arguments of addScript add a head:Boolean, true => put it int the by default, false put it before </body>

Actual result

Always in the head

avatar kossa kossa - open - 16 Dec 2014
avatar kossa kossa - change - 16 Dec 2014
Title
Cannot insert css/js files before
Cannot insert css/js files before /body
avatar Bakual
Bakual - comment - 16 Dec 2014

Are you referring to the "google" way of putting a script call just before the closing body tag?
That is indeed not possible with the current API.
Or why do you need to call it at this place?

avatar Bakual
Bakual - comment - 16 Dec 2014

Btw, if you want to load JS files, you can use async and defer tags to tell the browser that he can safely load the script later or parallel and doesn't have to stop rendering the page.
See https://api.joomla.org/cms-3/classes/JDocument.html#method_addScript

avatar kossa
kossa - comment - 16 Dec 2014

Yeah sure, but nothing in google,
Also I want to load it after loading of the page, a great way for optimization, if I'm working on webapp, and I have a lot of JS files, they will block my pages.

Also for other CMS like wordpress you can specify when you put your script : http://codex.wordpress.org/Function_Reference/wp_enqueue_script ;)

avatar Fedik
Fedik - comment - 16 Dec 2014

In the arguments of addScript add a head:Boolean, true => put it int the by default, false put it before </body>

why this is expected, when addScript do not support such thing, in which documentation you found it? :wink:

avatar Bakual
Bakual - comment - 16 Dec 2014

Also I want to load it after loading of the page, a great way for optimization, if I'm working on webapp, and I have a lot of JS files, they will block my pages.

The defer and async flags will not block your pages. They are exactly for that use case. Async means that the browser can load the file whenever he wants, in any order. Defer means he can load them after the page is loaded, but in order.
Actually, deferring and asynchronous loading of JS files is better than using the hackish trick to put them at the end of the page. All modern browsers support those flags. Basically only IE9 doesn't support it afaik, and for that browser it will just be a slower page.

avatar kossa
kossa - comment - 16 Dec 2014

@Bakual I used already Async, butt always wa have a problem some js files depend on others ;)
@Fedik, I proposed a solution :)

avatar Bakual
Bakual - comment - 16 Dec 2014

I used already Async, butt always wa have a problem some js files depend on others ;)

If they depend on other files, you should use defer and not async. Because defer will load them in order (after the page is loaded).

avatar dgt41
dgt41 - comment - 16 Dec 2014

@kossa This has been raised before, but there is a better proposal for joomla (needs mootools out of the picture though) here: #1227

avatar zero-24 zero-24 - close - 16 Dec 2014
avatar kossa kossa - close - 16 Dec 2014
avatar kossa kossa - change - 16 Dec 2014
The description was changed
Status New Closed
Closed_Date 0000-00-00 00:00:00 2014-12-16 15:46:01
avatar zero-24 zero-24 - change - 7 Jul 2015
Labels Added: ?

Add a Comment

Login with GitHub to post a comment