
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
- How to display 1 day ago in comments in laravel view
- How to print form data in laravel
- Display message with session flash using bootstrap alert class in laravel
- Get posts belongs to a specific user in Laravel
- Method Illuminate\Database\Eloquent\Collection::lists does not exist
- Laravel recursive function in controller
- How to check records exist in loaded relationship in Laravel blade view
- Page loader in laravel
- Credit card validation in laravel
- How to get specific columns using with method in laravel Eloquent relationship
- How to validate URL with https using regex in laravel
- Get comma separated email from input array
- Laravel create default admin user
- Class "App\Http\Controllers\Auth\Verified" not found
- Method Illuminate\Events\Dispatcher::fire does not exist
- Laravel get count with where condition
- How to add unique records in pivot columns of Laravel pivot table
- Update if exist else insert new record in laravel
- Method Illuminate\Http\Request::validated does not exist
- How to get the id of last record from collection object in laravel view
- How to run a specific seeder class in laravel
- Return redirect laravel not working
- Symlink(): No such file or directory
- There are no commands defined in the "route:" namespace
- Get ids in array from users table