user1705850 user1705850 - 2 months ago 22
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>

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

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

function openTab(evt, tabName) {

function manage tab selections activating appropriate

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?


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

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

The callback function is fired after the load has completed

More information about .load