2

Not sure what is going on here. It seems that jQuery is not "extended" from a base.html file to an external one.

I have:

#base.html

<!doctype html>
<html class="no-js" lang="en">

<head>
...
... # css imports
</head>

<body>

   # some fixed stuff

   {% block body %}

      # some stuff specific to base.html

   {% endblock %}

   # js imports at the end of the body

   <script src="static/js/jquery-3.1.0.min.js"></script>

    ... # various other js
</body>
</html>

Then I have another html file:

#test.html

{% extends "base.html" %}

{% block body %}

    # some stuff

   <script type="text/javascript">

        $(document).ready( function () {
             alert("foo")
         } );

    </script>

{% endblock %}

Now, I don't get any alert. However, if I use plain javascript it works as expected.

I noticed that if I import again jQuery in test.html, jQuery works fine. But what's the point of extending then?

There must be something that I'm missing. By the way, this seems to happen only with jQuery, all other javascript libraries seem to be extended fine.

3
  • Have you tried importing jQuery BEFORE the body block? Commented Nov 28, 2018 at 4:03
  • @MiniGunnR good call! It seems indeed to work if I place it for example in the head. If you care enough to explain why and post it as an answer I'll gladly accept it. Commented Nov 28, 2018 at 9:09
  • Glad it worked. I wrote an answer. Commented Nov 28, 2018 at 9:18

3 Answers 3

1

It's really very simple. When the following code runs, it needs to run using jQuery.

<script type="text/javascript">

    $(document).ready( function () {
         alert("foo")
     } );

</script>

However, your jQuery is being loaded AFTER these commands, whereas, it needs to be placed BEFORE that.

It's like you're drinking water from a glass before you're putting water in it. You need to put the water in the glass first and then drink it. I don't know if this example makes it easy or not.

Sign up to request clarification or add additional context in comments.

2 Comments

Man I feel so stupid. Looking back at it today it's obvious. I guess it's one of those things when you have been working too long and don't see straight anymore. :) Thanks tho!
We've all been through this. Not a big deal.
0

Add this line.I hope this will work.

<script type="text/javascript" src="{{ url_for('static', filename='js/jquery-3.1.0.min.js') 
}}"></script>

Comments

0

At the moment $(document).ready( function () { executes, $ hasn't been aliased to jQuery, which has yet to load.

2 Comments

Thanks for the answer, but I'm not sure I understand, can you expand a little bit? Isn't jQuery supposed to be loaded from the extended base.html? If not, is the only way to solve this simply re-calling the jQuery script tag in test.html? What about inheritance tho.
It might help to 'view source' or inspect in a browser to see how the page has flattened out. The body block is expanded before the bit that pulls in jQuery.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.