I'm having an issue understanding general javascript/jquery execution order, and where the proper place is to store your js. I'm trying to reorganize my code at the moment, and keep running into the same problems, that just get me more and more frustrated with all of javascript.
I'm building a Python+Flask webapp with Jinja2 templating, incorporating various jquery stuff all over. Initially I had some standard html code that looks a bit like this...
<html>
<head>
<script type="text/javascript" src="{{url_for('static',filename='js/jquery/jquery-1.11.2.min.js')}}"></script>
<script type="text/javascript" src="{{url_for('static',filename='js/bootstrap/bootstrap.min.js')}}"></script>
<script type="text/javascript" src="{{url_for('static',filename='js/utils.js')}}"></script>
<script type='text/javascript'>
# get hash from page
function getHash() {
var hash = window.location.hash;
var newhash = hash.slice(hash.search('_')).replace('_','#');
return newhash;
}
// Load ifu div element if hash present when page loads
$(function(){
var ifuhash = getHash();
var ifu = ifuhash.slice(ifuhash.search('#')+1);
$(String(ifuhash)).fadeIn();
if (hash.search('comment') >= 0) $(String(hash)).fadeIn();
});
... other js ....
</script>
</head>
<body>
{% include 'header.html' %}
</body>
</html>
This runs just fine. But since a good practice seems to be to put all JS inside their own files, so as not to clutter up the html, I'm trying to reorganize my current javascript code to what I think should work, but I'm running into problems.
the function getHash is common to several pages so I pulled that out and stuck it in a separate piece of js,called plateinfo.js, such that my code was now
<html>
<head>
<script type="text/javascript" src="{{url_for('static',filename='js/jquery/jquery-1.11.2.min.js')}}"></script>
<script type="text/javascript" src="{{url_for('static',filename='js/bootstrap/bootstrap.min.js')}}"></script>
<script type="text/javascript" src="{{url_for('static',filename='js/utils.js')}}"></script>
<script type="text/javascript" src="{{url_for('static',filename='js/plateinfo.js')}}"></script>
<script type='text/javascript'>
// Load ifu div element if hash present when page loads
$(function(){
var ifuhash = getHash();
var ifu = ifuhash.slice(ifuhash.search('#')+1);
$(String(ifuhash)).fadeIn();
if (hash.search('comment') >= 0) $(String(hash)).fadeIn();
});
... other js ....
</script>
</head>
<body>
{% include 'header.html' %}
</body>
</html>
and plateinfo.js looks like
// Toggle add comment login function
$(function() {
$('#addcommentbut').click(function() {
var fxn = 'grabComments';
$('#fxn').val(fxn);
});
});
// Get IFU hash from page
function getHash() {
var hash = window.location.hash;
var newhash = hash.slice(hash.search('_')).replace('_','#');
return newhash;
}
// Render Tags in div
function renderTags(hash) {
..does stuff
}
However, when I do this, I now get the error
ReferenceError: getHash is not defined
The function is outside the DOM scope and should be importable. And I have done this successfully before. The utils.js file contains standalone functions that work perfectly. I've cleared my cache, restarted my browser to refresh the session, restarted my computer, but nothing seems to work. So what's the deal? I thought the execution order of html/js code was direct top-to-bottom. So what's the best practice here? What's the right import/execution order?
Update to show execution order:
So I put in a bunch of console.logs like so
<html>
<head>
<script type="text/javascript" src="{{url_for('static',filename='js/jquery/jquery-1.11.2.min.js')}}"></script>
<script type="text/javascript" src="{{url_for('static',filename='js/bootstrap/bootstrap.min.js')}}"></script>
<script type='text/javascript'>$(function() {console.log('before utils.js');});</script>
<script type="text/javascript" src="{{url_for('static',filename='js/utils.js')}}"></script>
<script type='text/javascript'>$(function() {console.log('after utils.js');});</script>
<script type='text/javascript'>$(function() {console.log('before plateinfo.js');});</script>
<script type="text/javascript" src="{{url_for('static',filename='js/plateinfo.js')}}"></script>
<script type='text/javascript'>$(function() {console.log('after plateinfo.js, before last script');});</script>
<script type='text/javascript'>
console.log('start of last script');
// Load ifu div element if hash present when page loads
$(function(){
console.log('calling gethash');
var ifuhash = getHash();
var ifu = ifuhash.slice(ifuhash.search('#')+1);
$(String(ifuhash)).fadeIn();
if (hash.search('comment') >= 0) $(String(hash)).fadeIn();
});
... other js ....
</script>
</head>
<body>
{% include 'header.html' %}
</body>
</html>
and here's the log output
before utils.js plateInfo.html:40:1
after utils.js plateInfo.html:44:1
start of last script plateInfo.html:204:5
before plateinfo.js plateInfo.html:185:3
after plateinfo.js, before last script plateInfo.html:189:15
calling gethash plateInfo.html:221:3
ReferenceError: getHash is not defined plateInfo.html:222:6
Why is this?
Aside: I'm also having separate but related issues with referencing my Jinja2 template variables inside javascript. This is happening with this header.html file I'm including. If I include all the js code inside the same html file, it works fine. But when I move the js to a separate header.js, the template variables no longer are referenced properly. Maybe this deserves a separate post.