3

I use a basic.html in my flask application which loads all needed ressources like footer navbar etc., all my other html sites extend this basic.html

In my main.js I created a simple function:

function fun() {
  alert("we have fun");
}

I load my files like this in the basic.html:

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

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

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

Now I want to use this function in my page.html which extends the basic.html:

{% extends "basic.html" %} 

{% block content %}

<div class = "container" onload="fun()">
<p> Test </p>
</div>

{% endblock %}

Sadly nothing happens, but if I use the onload="fun()" in the basic.html itself it works.

So how do I resolve this?

This is a very simplified version of one of my previous question, due to the fact that I am sitting on this since 4 hours I think I know what causes the issue but I cant solve it and all solutions I found do not work.

Here is my previous question: question

I think that onload="fun()" is called before jQuery is even load or my main.js but I am still not 100% sure if this causes the error and I cant find a solution

2 Answers 2

3

The problem is simple. You can't attach onload to a div.

The onload event can only be used on the document(body) itself, frames, images, and scripts. In other words, it can be attached to only body and/or each external resource. The div is not an external resource and it's loaded as part of the body, so the onload event doesn't apply there.

Adding the script tag immediately after the div element will cause it to fire as soon as the div is rendered. So this will perfectly work:

{% extends "basic.html" %}
{% block content %}

<div class="container">
<p> Test </p>
</div>

<script type="text/javascript">
fun();
</script>

{% endblock %}

Read more here: How to add onload event to a div element?

Instead of appending a script tag with the function call, you can use another solution explained here: https://stackoverflow.com/a/17071878/4395646

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

6 Comments

seems like he wants to keep javascript code in a separate file so the problem is how to connect js file with the html file
@leela.fry I'm sorry if it wasn't obvious. But he still keeps the javascript code in main.js, just calls it after the div. I've edited the code to clear it up.
yeah that was stupid to attach it to div, I already figured it out, it works if I connect in to a button for example and use onclick, so the scripts are loaded succesfully, but my main problem is the one in the first quistion, it seems that google apis do not work on localhost
@Roman if this problem has been fixed please accept the answer. I'll take a look on your other question too! Thank you.
I am sure it simply does not work on localhost, the Google JavaScript Api
|
2

Put that in your basic.html

<head>

<script src="{{url_for('static', filename='js/main.js')}}"></script>

</head>

<body onLoad="fun();">

onLoad needs to go in the body not in div

3 Comments

This will work under the presumption you have set your static path to static folder and inside of it have a folder named JS
This answer is not useful, since you're altering a parent template for an effect desired on the child template. This will cause EVERY page rendered using child template to call fun() which is clearly not desirable.
@MiteshNinja if you want to make people sign up for your page that may be what we want, it shows something that my be useful in a real project

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.