Javascript inclusion issues

  • Posts: 82
  • Thank you received: 1
11 years 2 months ago #161208

-- HikaShop version -- : Business 2.3.1
-- Joomla version -- : 3.3.1
-- PHP version -- : 5.3.x

Hello guys,
just updated hikashop from 2.2.3 to 2.3.1 and Joomla from 3.2.x to 3.3.1

But I started getting JS errors, especially in the Product Pages.
The main error was a conflict with the bootstrap.js:
TypeError: 'undefined' is not a function (evaluating 'e(document).on("click.alert.data-api",t,n.prototype.close)')

Checked the head of the document, and found that scripts are loaded in this order:
<script src="/media/com_hikashop/js/hikashop.js?v=231" type="text/javascript"></script>
<script src="/media/system/js/mootools-core.js" type="text/javascript"></script>
<script src="/media/jui/js/jquery.min.js" type="text/javascript"></script>
<script src="/media/jui/js/jquery-noconflict.js" type="text/javascript"></script>
<script src="/media/jui/js/jquery-migrate.min.js" type="text/javascript"></script>
<script src="/media/system/js/core.js" type="text/javascript"></script>
<script src="/media/system/js/mootools-more.js" type="text/javascript"></script>
<script src="/media/system/js/modal.js" type="text/javascript"></script>
<script src="/modules/mod_artzoomin/js/zoom_vm.js" type="text/javascript"></script>
<script src="/media/jui/js/bootstrap.min.js" type="text/javascript"></script>
<script src="/templates/template/js/gk.scripts.js" type="text/javascript"></script>
<script src="/templates/template/js/gk.menu.js" type="text/javascript"></script>
<script src="/media/sourcecoast/js/jq-bootstrap-1.8.3.js" type="text/javascript"></script>

which is creating the conflicts. The Products Page is messing up with the inclusion order of the scripts and this is caused by the way they are included in the view.html file.

Probably not all Joomla 3.x websites/templates will use the Bootstrap or jQuery frameworks, but I guess it will be the majority of them, but ensuring that Bootstrap or jQuery is included first and in no conflict mode, before your scripts, it would lead to less issues and save you time supporting users.
To do so, you can check if the Bootstrap or jQuery libraries are included and use the JHTML class at the beginning of your views file, to load those libraries at the top of the scripts, before any other script.
With JHTML you will always be sure that the required library will be loaded only once and before your other scripts.

In my case, I had to use the JHtml::_('bootstrap.framework') in the products view.html.php file.
After that the order of the scripts is okay:
<script src="/media/com_hikashop/js/hikashop.js?v=231" type="text/javascript"></script>
<script src="/media/jui/js/jquery.min.js" type="text/javascript"></script>
<script src="/media/jui/js/jquery-noconflict.js" type="text/javascript"></script>
<script src="/media/jui/js/jquery-migrate.min.js" type="text/javascript"></script>
<script src="/media/jui/js/bootstrap.min.js" type="text/javascript"></script>
<script src="/media/system/js/mootools-core.js" type="text/javascript"></script>
<script src="/media/system/js/core.js" type="text/javascript"></script>
<script src="/media/system/js/mootools-more.js" type="text/javascript"></script>
<script src="/media/system/js/modal.js" type="text/javascript"></script>
<script src="/modules/mod_artzoomin/js/zoom_vm.js" type="text/javascript"></script>
<script src="/templates/template/js/gk.scripts.js" type="text/javascript"></script>
<script src="/templates/template/js/gk.menu.js" type="text/javascript"></script>
<script src="/media/sourcecoast/js/jq-bootstrap-1.8.3.js" type="text/javascript"></script>

Please Log in or Create an account to join the conversation.

  • Posts: 84298
  • Thank you received: 13697
  • MODERATOR
11 years 2 months ago #161227

Hi,

HikaShop doesn't need bootstrap to work.
It only needs it when the "use bootstrap design" option is turned on in the configuration (it's off by default), and as the documentation on that option says,

It requires that your Joomla template supports bootstrap

meaning that in that case, it should the role of the template to load bootstrap, like it is with the default Joomla templates.
It's not the role of HikaShop to always load bootstrap on the frontend and doing that might lead to breaking the template javascript (for example if it uses another version of bootstrap than joomla).
So your solution works in your case, but that's not something we could add in HikaShop as it would actually create more problems than anything.

Please Log in or Create an account to join the conversation.

  • Posts: 82
  • Thank you received: 1
11 years 2 months ago #161275

Hello Nicolas,

I know Hika doesn't need bootstrap. However jQuery/Bootstrap will probably exist in a Joomla 3 website, either by the template or another extension. So it is important to be able to keep the right order of the scripts in the head of the document.

But currently hikashop injects mootools in the head and disturbs the scripts order. You can see what is the final order of the scripts in the product page in my first post.
And It is not only if the template will load the other libraries, and I am not asking from hikashop to load jQuery instead of my template that makes use of it. I am just asking for hikashop to try to respect that I already use jQuery/Bootstrap.
The extensions should do their best to ensure that there will be no conflicts with the javascript and obey in the hierarchy those scripts have to be loaded.

That's why I said that it will be good to check if a website already uses bootstrap/jquery, so you can use the JHTML class to include those libraries first in your views, before injecting other JS.

Maybe it won't be that easy, or it won't give accurate results to check this from within your extension's code, but it can be just as simple as a backend "Javascript Setting" - "Does your website uses already?", with options like:

  1. jQuery
  2. Bootstrap
  3. Both libraries
  4. None
And then depending on the choice, the associated code will make sure that the right libraries will be loaded in the right order in your views.

JHTML will not load twice any library, but it will make sure that this library will be loaded before any other libraries.
This is a minimum addition (just a few lines of code, and you only have about 10-11 views in total), but will give maximum result in terms of compatibility in a complex environment with many extensions, especially since Bootstrap/jQuery will be there in the majority of the websites.
Think about it...

* Okay, we can use 3rd party plugins to set the order of the scripts in the head, but why to use plugins when something can be improved and work without such issues?

Last edit: 11 years 2 months ago by jmbgreen.

Please Log in or Create an account to join the conversation.

  • Posts: 84298
  • Thank you received: 13697
  • MODERATOR
11 years 2 months ago #161334

Hi,

HikaShop already uses JHTML to load the javascript libraries it needs when it needs them.
I don't see the point of loading libraries that HikaShop doesn't need all the time.
It's not the role of HikaShop to fix the ordering of the libraries on the website because some other extension might not load its stuff properly or might not use the libraries as recommended for better compatibility.
Each extension which needs to load a library should load it according to the recommendations (meaning with no-conflict, without using directly $, etc) so that it doesn't affect other extensions.
Mootools not having a no-conflict mode, all the other libraries loading need to make sure that they can work regardless of when Mootools is loaded on the header by not using directly $, etc. That's apparently not the case on your website which is why you found the work around of switching the order of the library loading so that it doesn't break.

In the long term, our goal is to completely remove the use of mootools in HikaShop. So the issue will be moot since other extensions not loading libraries properly to handle mootools won't have the problem anymore.

Please Log in or Create an account to join the conversation.

Time to create page: 0.049 seconds
Powered by Kunena Forum