Uahnbu Tran
Uahnbu Tran

Reputation: 109

How to start window.onload "onload"?

I'm making a website with unchanged header and footer. The main div is changed when one clicks the sites. Here is the code:

<p id="item1">Item 1</p>
...
<script>
  $("#item").click(function(){$("#main").load("item1.html");});
</script>

In item1.html:

<div id="sth"></div>
...
<script>
  window.onload = function() {document.getElementById("sth").style.width = ...
</script>

Item1.html works fine when I open it separately (url = mywebsite.com/item1.html). But when I start it with the script above (index.html), the window.onload function don't work. How can I fix it?

Upvotes: 1

Views: 456

Answers (1)

Krish R
Krish R

Reputation: 22711

You can use .load success callback function as mentioned below

$("#item").click(function(){
   $("#main").load("item1.html", function(){
          // your logics here 
         document.getElementById("sth").style.width =...
    });
});

You can understand the use of window.onload

Upvotes: 2

Related Questions