contao-components/jquery
Provides the jQuery library as a Contao component/package for easy inclusion in Contao projects. Useful for managing a consistent jQuery version, updating via Composer, and integrating jQuery-dependent scripts without manual downloads or asset handling.
Installation Add the package via Composer:
composer require contao-components/jquery
Publish the assets (if needed) via:
php artisan vendor:publish --tag=contao-components.jquery
Basic Usage Include jQuery in your Blade template:
@jquery
This will output the jQuery script tag with the correct version (default: latest stable).
First Use Case Use jQuery in a simple DOM manipulation task:
$(document).ready(function() {
$('#example-button').click(function() {
alert('Button clicked!');
});
});
Ensure your script is loaded after jQuery:
@jquery
@scripts
Asset Management
@jquery in your Blade layouts to ensure jQuery is loaded globally.resources/js/ and compile with Laravel Mix or Vite.Version Control
Override the default jQuery version in config/contao-components/jquery.php:
'version' => '3.6.0', // Example: Pin to a specific version
Conditional Loading Load jQuery only on specific routes or views:
@if(Request::is('admin/*'))
@jquery
@endif
Integration with Frontend Frameworks
@jquery in your root layout and ensure no conflicts with $ by using jQuery in no-conflict mode:
jQuery(document).ready(function($) {
// Use $ safely here
});
Laravel Mix/Vite Setup
webpack.mix.js:
mix.js('resources/js/app.js', 'public/js')
.jquery(); // Optional: If using Laravel Mix's built-in jQuery loader
main.js:
import $ from 'jquery';
Duplicate Scripts
@jquery once in your layout and avoid manual <script> tags for jQuery.Version Mismatches
console.log(jQuery.fn.jquery); // Check loaded version
No-Conflict Mode
$ alias. Use no-conflict mode:
var jQuerySafe = $.noConflict();
jQuerySafe(document).ready(function() {
// Use jQuerySafe instead of $
});
Asset Compilation Issues
@jquery.Check Loaded Scripts
Inspect the <head> or <body> of your rendered page to confirm jQuery is loaded:
<script src="https://code.jquery.com/jquery-3.x.x.min.js"></script>
Console Errors
$ is not defined
This indicates jQuery wasn’t loaded or was overridden.Clear Cached Views After publishing config or assets, clear Laravel’s view cache:
php artisan view:clear
Custom CDN or Local Path
Override the jQuery source in config/contao-components/jquery.php:
'source' => [
'cdn' => 'https://your-cdn.com/jquery.min.js',
'local' => 'path/to/local/jquery.min.js',
],
Add jQuery Plugins Extend the package by creating a custom Blade directive or helper:
// In a service provider
Blade::directive('jqueryPlugins', function ($expression) {
return "<script src='/js/plugins/{$expression}.js'></script>";
});
Usage:
@jquery
@jqueryPlugins('datatables')
Dynamic Loading Use Alpine.js or Laravel Livewire to conditionally load jQuery only when needed:
<div x-data="{ loadJQuery: false }">
<button @click="loadJQuery = true">Load jQuery</button>
<template x-if="loadJQuery">
@jquery
<script>
// jQuery code here
</script>
</template>
</div>
How can I help you explore Laravel packages today?