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
- Use of undefined constant laravel
- How to check record exist or not in relationship table
- How to use bootstrap pagination in laravel 8
- Send post data from controller to view
- Extract only time from datetime in laravel
- How to add background image to div using Tailwindcss, Vite in Laravel Environment
- How to include header file in laravel
- Remove several global scope from query
- Update existing pivot table data in laravel
- Ignore Records where a field has NULL value in Laravel
- How to get all posts which contains comments in laravel
- Fatal error: Composer detected issues in your platform: Your Composer dependencies require a PHP version ">= 8.0.0"
- How to get query string value in laravel
- SQLSTATE[42000]: Syntax error or access violation: 1075 Incorrect table definition; there can be only one auto column and it must be defined as a key
- How to create controller in laravel
- Laravel csrf token mismatch for ajax POST Request
- Get last year created records in Laravel
- How to get all route list
- How to get last year records count with month wise in Laravel
- Use withCount() to Calculate Child Relationship Records
- Display success message in laravel
- Sample configuration files to create laravel project with docker using wsl (window subsystem linux)
- Get duplicate records in laravel
- How to call model in blade laravel
- Drop foreign key column in Laravel using migration