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
- Send post data from controller to view
- How to call model in blade laravel
- Link storage folder in laravel 8
- Laravel 9 pagination with search filter
- How to return a column with different name in Laravel
- Non-static method App\Http\Helper::myFunction() should not be called statically
- Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails
- SQLSTATE[42000]: Syntax error or access violation: 1055
- Define variable and use in Laravel controller method
- Permission denied error while creating storage link in Laravel
- How to randomly get the user id from users table in laravel
- How to validate website url in laravel using validaiton
- Route not defined in Laravel
- How to check column value of a record is null or not in laravel
- How to add dynamic page title in Laravel view
- The use statement with non-compound name 'DB' has no effect
- Route [password.request] not defined
- After image selected get validation error in laravel
- SQLSTATE[23000]: Integrity constraint violation: 1022 Can't write; duplicate key in table
- Laravel create default admin user
- How to get last month records in Laravel
- Global scope in Laravel with example
- If condition in Laravel 9
- How to get data from two tables in laravel
- Rename Pivot Table in Laravel