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
- Get posts belongs to a specific user in Laravel
- How to check query string exists or not in laravel blade
- Display first n record from collection in laravel view
- Laravel insert query not working
- Multiple Level eager loading in Laravel
- How to customize pagination view in laravel
- Display success message in laravel
- Laravel 7 login error message not showing
- How to check record exist or not in relationship table
- Laravel create table migration with model
- The openssl extension is required for SSL/TLS protection but is not available
- Update record after find method in lavavel
- Skip first n record and display rest records in laravel view
- Add class to body in laravel view
- How to automatically update the timestamp of parent model in Laravel
- Send id with route Laravel
- How to include header file in laravel
- Get comma separated email from input array
- How to Access Array in blade laravel
- How to upload local Laravel project to server ?
- Split an Eloquent Collection by half in Laravel
- Submit form without CSRF token in Laravel
- Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails
- Sample .htaccess file and index.php file under public directory in laravel
- Laravel API response format