user1705850 user1705850 - 10 days ago 6
Javascript Question

Importing an html page in a div and executing "body onload" [SOLVED]

Sorry for the easy question, I'm a html/js newbie.

I have an html page "B.html" with:



<body onload="init()" >
-----other html/js stuffs







that is loaded fine in browser.

I include B.html inside a div in "A.html" (tab) using:



<ul class="tab">
<li><a href="javascript:void(0)" class="tablinks" onclick="openTab(event, 'Main')">Main</a></li>
.........
</ul>

<div id="Main" class="tabcontent"> </div>
.........

<script>
$(function(){ $("#Main").load("B.html"); });

function openTab(evt, tabName) {
.........
</script>





openTab
function manage tab selections activating appropriate
div


Now, opening B.html all things work fine.

Embedding B.html inside A.html as I wrote before, works except for js function that need to be initialized by "init". I cannot obviously use
body onload="init()"
because I'm already inside body of A.html

How I can do this?

Answer

Call the init function in the callback function of the load:

$("#Main").load("B.html", function() {
    init();
});

The callback function is fired after the load has completed

More information about .load