I want to load most of my scripts in my main layout ( i.e jquery). Now from my understanding it is best practice to position scripts at the bottom of my html page.
However if we put the script at the bottom of the layout page like so.
layout/default.html.ep
<!doctype html>
<html>
<head><title><%= title %></title></head>
<body><%=content %></body>
</html>
<script src="js/jquery.min.js" type="text/javascript"></script>
And then use this layout in a page that has its own javascript that relies on jquery like so.
testscript.html.ep
%layout 'default';
%title 'Script Test';
<p>Main Page</p>
<script type="text/javascript">
$(document).ready(function(){
alert('fails if jquery is not loaded');
});
</script>
You end up with a page like this. Notice that the reference to jquery is BELOW my code that relies on it.
<!doctype html>
<html>
<head><title>Script Test</title></head>
<body>
<p>Main Page</p>
<script type="text/javascript">
$(document).ready(function(){
alert('fails if jquery is not loaded');
});
</script>
<script src="js/jquery.min.js" type="text/javascript"></script>
</body>
</html>
What is the best way to deal with this situation?
Put my javascript references at the top of my layouts? I assume it's not best practice to add the script reference for jquery in every page that uses this layout?
Any help is much appreciated. I'm very much a new comer to all of this.
cheers.