2

I am trying to display an offer when the page loads.

The offer works and displays using the onClick method (meaning there is nothing wrong with the 'getOffers.php' script:

<div class="container" onclick="getRequest('<?=$root?>getOffers.php')">

    <div id="offers" class="home-box">
        <h3>Offers</h3>
        <p id="target"></p>
    </div><!-- /#offers -->

</div><!-- /.container -->

Whenever i try:

<div class="container" onload="getRequest('<?=$root?>getOffers.php')">

it doesn't seem to work, could somebody kindly tell my what i am doing wrong.

5 Answers 5

3

As you can see at https://developer.mozilla.org/en-US/docs/Web/API/GlobalEventHandlers.onload , onload is an event of the window. You cannot use it on a div.

Sign up to request clarification or add additional context in comments.

Comments

1

put the onload call on the <body> element or just put it in a <script> tag below the div

<div class="container">
...
</div>
<script type="text/javascript">
     getRequest('<?=$root?>getOffers.php');
</script>

Comments

0

The "load" event is not fired for <div> elements. You can wait for the "load" event at the <body> or document or window.

Comments

0
window.onload = function(){
   // do your stuff...
}

Note that if you override onload function, you will lose your previous assignement.

Comments

0

try to call onload of body of html

<body onload="getRequest('<?=$root?>getOffers.php')">

The onload event occurs when an object has been loaded.

onload is most often used within the <body> element to execute a script once a web page has completely loaded all content (including images, script files, CSS files, etc.). so you can't apply it on the div


http://www.javascriptkit.com/javatutors/event3.shtml

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.