jQuery in 5.7

Permalink 1 user found helpful
Hi,

Does 5.7 use a customised version of jQuery?
How can I swap it for my own version?

Thanks
Dave

madesimplemedia
 
MrKDilkington replied on at Permalink Reply
MrKDilkington
Hi madesimplemedia,

I believe the jQuery supplied by concrete5 is an unmodified version of v1.11.3.

You can use your own version by supplying it in your theme assets and providing it in your theme page_theme.php. This is done within the registerAssets() method using $this->providesAsset('javascript', 'jquery');.
madesimplemedia replied on at Permalink Reply
madesimplemedia
Thanks mate, I'll play around with that.
ob7dev replied on at Permalink Reply
ob7dev
Make sure to only load your version of jQuery when your not in edit mode, otherwise there will be conflicts and there will be issues with inline editing.

Heres how I do it in a custom template, where I import jquery only if 'concrete' is not in edit mode:
<?php
if (!$c->isEditMode()) { ?>
    <script src="<?= $view->getThemePath();?>/js/jquery-2.2.0.min.js"></script>
<?php } ?>

And here is more on dealing with assets in concrete5.7:
http://documentation.concrete5.org/developers/assets/overview...
http://documentation.concrete5.org/developers/working-with-blocks/w...
MrKDilkington replied on at Permalink Reply
MrKDilkington
@ob7dev

You are correct that using jQuery 2/3 requires additional steps. It still needs to be required/provided in your page_theme.php though. If you don't tell concrete5 that you are providing jQuery in your theme, and a block requires it, concrete5 will load its internal copy. This can lead to multiple copies of jQuery being loaded.

Example:
- in the include you use at the top of your page templates
<?php
// conditionally load jQuery 2.x based on whether the toolbar is visible or a user is logged in
$currentPermissions = new Permissions($c);
$user = new User;
if (!$currentPermissions->canViewToolbar() || !$user->isLoggedIn()) { ?>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script>
<?php } ?>

- in your theme page_theme.php
// conditionally load jQuery 1.x or jQuery 2.x based on whether the toolbar is visible or a user is logged in
$c = \Page::getCurrentPage();
$currentPermissions = new \Permissions($c);
$user = new \User;
if ($currentPermissions->canViewToolbar() || $user->isLoggedIn()) {
    // concrete5 loads its internal copy of jQuery 1.x
    $this->requireAsset('javascript', 'jquery');
} else {
    // you provide a copy of jQuery 2.x with your theme
    $this->providesAsset('javascript', 'jquery');
}
ob7dev replied on at Permalink Reply
ob7dev
Where would the jquery2 file be loaded? How does page theme know to use jquery2 if both providesAsset and requireAsset say 'jquery'?
ob7dev replied on at Permalink Reply
ob7dev
I think I figured this out. ProvidesAsset is basically telling Concrete not to load its internal copy of 'jquery'. Its like saying "DontProvideAsset". I then provide jquery myself in my theme. Is that correct?
MrKDilkington replied on at Permalink Reply
MrKDilkington
@ob7dev

- If you "provide" jQuery with your theme, concrete5 will not load its own copy.
- If you "require" jQuery from concrete5, concrete5 will load its own copy.