1

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?

royhowie
  • 11,075
  • 14
  • 50
  • 67
Uahnbu Tran
  • 109
  • 9

1 Answers1

2

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

Community
  • 1
  • 1
Krish R
  • 22,583
  • 7
  • 50
  • 59