Jenny
Jenny

Reputation: 1727

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)?

Upvotes: 6

Views: 60007

Answers (5)

GorvGoyl
GorvGoyl

Reputation: 49210

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);

Upvotes: -1

Muhammad Bilal
Muhammad Bilal

Reputation: 1152

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");
});

Upvotes: -2

Matt Cashatt
Matt Cashatt

Reputation: 24208

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
    });

Upvotes: 7

Roman
Roman

Reputation: 10403

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.

Upvotes: 1

Jason
Jason

Reputation: 15931

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.

Upvotes: 3

Related Questions