
How to add script on specific view file in laravel while extending layout
How to add script on specific view file in laravel while extending layout
Through the use of the programming language, we will work together to solve the "How to add script on specific view file in laravel while extending layout" puzzle in this lesson.
Sometimes we need to add a script on a specific view file which we can’t add to the layout file then we can use @stack and @push method to add script on specific view file in laravel while extending layout file.-
Add fixed header script code to a specific view file using @stack and @push
//resources\views\layouts\<app>.blade.php //Add before the closing of body tag @stack('child-scripts') //resources\views\<home>.blade.php //Add at the bottom of child view file @push('child-scripts') @include('partials.js.header_fixed') @endpush //resources\views\partials\js\header_fixed.blade.php //Javascript code to fixed header <script> document.addEventListener("DOMContentLoaded", function(){ window.addEventListener('scroll', function() { if (window.scrollY > 50) { document.getElementById('navbar_top').classList.add('fixed-top'); // add padding top to show content behind navbar navbar_height = document.querySelector('.navbar').offsetHeight; document.body.style.paddingTop = navbar_height + 'px'; } else { document.getElementById('navbar_top').classList.remove('fixed-top'); // remove padding top from body document.body.style.paddingTop = '0'; } }); }); </script>
This code snippet will help you to add a particular script code to a specific view file on which you want to add while extending the layout file in laravel. Sometimes we don’t need to add a script on all pages while extending a particular layout. This code snippet will work for you in case you don’t want to add a particular script on every page.
If you like what you are reading, please consider buying us a coffee ( or 2 ) as a token of appreciation.
Don't forget to share this article! Help us spread the word by clicking the share button below.
We appreciate your support and are committed to providing you valuable and informative content.
We are thankful for your never ending support.
Random Code Snippet Queries: Laravel
- Array to string conversion laravel blade
- Input file with max size validation in laravel
- Get current month records in laravel 7/8
- Method Illuminate\Http\Request::validated does not exist
- Use withCount() to Calculate Child Relationship Records
- How to insert dynamic values to additional column with pivot column in pivot table on multiple records
- Non-static method App\Http\Helper::myFunction() should not be called statically
- Eager loading dynamically in laravel
- Route prefix with auth middleware in laravel
- Split an Eloquent Collection by half in Laravel
- How to check records exist in loaded relationship in Laravel blade view
- Symlink(): No such file or directory
- How to generate .env file for laravel?
- Laravel migration add foreign key to existing table
- Fatal error: Composer detected issues in your platform: Your Composer dependencies require a PHP version ">= 8.0.0"
- How to get route method name in Laravel
- Cannot end a section without first starting one
- How to pass query string with pagination in laravel
- Comment .env file in laravel
- Laravel create multiple records in Pivot table
- How to add unique records in pivot columns of Laravel pivot table
- How to add columns in existing table using migration in laravel
- How to validate form input data in laravel
- Send OTP using textlocal api in laravel
- Display data in table using foreach in Laravel