Question

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.

Was it helpful?

Solution 2

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>

OTHER TIPS

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.

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

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

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

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top