Scripts loaded in default template not working in views
Hello!
I have a problem with scripts (or js frameworks) when I load it in my main template these scripts not working in my view when extend it. I`ll try to show an example:
@include('partials.head') --> Have the header and I load CSS and some others.
@yield('content') -> Where the views are rendered.
@include('partials.footer') -> The page footer.
@include('partials.scriptsFooter') -> Load jQuery and close </body></html>
In my view I have:
@extends('app')
@section('content')
Any content of view here
<script>
$(document).ready(function() { --> not working give the error: Uncaught ReferenceError: $ is not defined
});
</script>
@endsection
The curious if I load jQuery in my view its work like a charm, but if I do it, i am just load jQuery twice (template and view)
In your web browser right click and inspect element. You'll probably find 404 errors for your CSS and jQuery files not found.
As your application grows the files get deeper and your URL or src isn't correct.
Easiest way is pull in Illuminate Html package and utilize the html::style and script methods. Or you can use the URL::to method right out of the box.
If you include JQuery (and other scrips) in your partials.scriptsFooter and try to call $(document).ready() before that you will get the $ is not defined error because you are trying to use JQuery before actually importing it.
Solution: either place your scripts in a .js file that you import after you import JQuery or simply move JQuery to your partials.head below the CSS.
@Marlon Actually I wouldn't recommend loading JQuery in the head section as perkola suggests.
It's best practice to load your javascripts at the end of the document so that it doesn't delay the loading of the rest of your page, amongst other things.
Just ensure that JQuery is loaded before other scripts that rely on it. Generally it should be the first script loaded.
Adding to what's already been said, if you really need/want to have your script in your view, add it to another section that gets yielded into your partials.scriptsFooter and it will behave the same as if you were to have put it there directly.
In other words:
partials.scriptsFooter:
<script src="//code.jquery.com/jquery-2.1.3.min.js"></script>
<!-- Other scripts here... -->
@yield('view.scripts')
</body>
</html>
your-view.blade.php:
@extends('app')
@section('content')
<!-- This content will be inserted into the layout in place of the yield(content) -->
Any content of view here
@endsection@section('view.scripts')
<script>
$(document).ready(function() {
// This will now be added just before the closing body tag, after jquery,
// and thus should work fine.
});
// However, to not worry about potential collisions if you were to use multiple
// js libraries that want to use the dollar sign identifier, you might be better off
// doing something like this, and running jQuery in no conflict mode:
(function($) {
// your normal jQuery code goes here.
$(document).ready(function() { /* Do stuff */ });
})(jQuery);
</script>
@endsection