I want to execute a basic javascript function once my webpage has fully loaded. Here's an example of the function:
<script type="text/javascript">
function changeSize() {
var el = document.getElementById("my-id");
el.style.height = "500px";
};
</script>
My webpage contains a lengthy (takes seconds) script fetched from an external URL that generates the main content within the html body.
I am utilizing Bootstrap and the body section in my base.html
looks like this:
<body>
<div class="container-fluid">
{% block header %}{% endblock %}
{% block content %}{% endblock %}
{% block footer %}{% endblock %}
</div>
<!-- jQuery first, then Popper.js, then Bootstrap JS -->
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js" integrity="sha384-ZMP7rVo3mIykV+2+9J3UJ46jBk0WLaUAdn689aCwoqbBJiSnjAK/l8WvCWPIPm49" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js" integrity="sha384-ChfqqxuZUCnJSK3+MXmPNIyE6ZbWh2IMqE241rYiqJxyMiZ6OW/JmZQ5stwEULTy" crossorigin="anonymous"></script>
</body>
The content block is imported from an external html file as shown below:
{% extends 'base.html' %}
{% block header %}
{% include 'header.html' %}
{% endblock %}
{% block content %}
<div class="my-class" id="my-id">
{{ myapp | safe }}
</div>
{% endblock %}
{% block footer %}
{% include 'footer.html' %}
{% endblock %}
Within the 'my-id'
element, myapp
is populated by the long-loading js script which, upon completion, displays all page content. The external URL path for myapp
is defined and retrieved via a python script using Flask.
I have attempted window.onload = changeSize;
,
<body onload="changeSize();">
, and monitored the timing of all stages in document.readyState
.
All these methods trigger my js function after the html has fully loaded, but before the external script for myapp
finishes. How can I detect when all elements and scripts have completely finished loading?