Question

[Solved] How to make a div onload function?

I want to load an HTML page as a div inside my webpage by removing its HTML and body tags. But the HTML page has a <body onload=" ... " > , I need this function to continue working. Seems <div onload=" ... " > is not working. How can I insert this onload function into my website’s body (on this page only) without directly editing my original website code (php)?

Enquirer: Jenny

||

Solution #1:

you can use jQuery.load to load the contents of the page into your div

$(document).ready(function() {
  $("#containing-div").load("[url of page with onload function]");
});

the code above goes in the page that contains the div. the page with the onload function doesn’t get changed.

Respondent: Jason

Solution #2:

Have you used jQuery before? If so, just get the id of your div (let’s say “SomeDiv”) and then use the “ready” method like this:

$("#SomeDiv").ready(
function(){
     //do stuff
    });
Respondent: Matt Cashatt

Solution #3:

You can add an additional Javascript tag at the end of the loaded page (once inserted inside the div) which will executing as soon as it’s loaded. Like this:

<div>
    Insert the inner html content of that pages here and add this script at the bottom and add the onload function of the original html to this script.
    <script type="javascript">
       alert("hello world");
    </script>
</div>

Just remember to have the javascript available to your page. What I mean is that if the javascript function called which is called inside onload="..." is defined in the <head> of the loading html document and you’re throwing the <head> out then this won’t work.

Respondent: Roman

Solution #4:

Not the best but one way is to check the div periodically if it’s loaded:

var myInterval = setInterval(function () {
if ($('.mydiv') && $('.mydiv').width() > 0) {
// write your logic here
clearInterval(myInterval);
    } 
}, 3000);
Respondent: GorvGoyl

Solution #5:

If you want to add the onload event on a div, you cannot, but you can add onkeydown and trigger the onkeydown event on document load.

<div onkeydown="setCss();"></div>

$(function () {
    $(".ccsdvCotentPS").trigger("onkeydown");
});
Respondent: Muhammad Bilal

The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 .

Most Popular

To Top
India and Pakistan’s steroid-soaked rhetoric over Kashmir will come back to haunt them both clenbuterol australia bossier man pleads guilty for leadership role in anabolic steriod distribution conspiracy