Search code examples
javascriptajaxprototypejsdom-events

Observe Event 'Window Onload' within a refreshed DIV-element


I have a div which is getting reloaded with an ajax update (when pressing on the a href link "CLICK HERE!").

In this div there is an event observer for "window onload". This event only fires when the page is getting reloaded in the browser, not when hitting on "CLICK HERE". Although evalScripts is set to true.

The function "refreshDiv":

new Ajax.Updater('ajax', ajaxUrl, {
        method: 'post',
        parameters: {...},
        evalScripts: true,
        onComplete: function() {..}
}

The HTML:

<div id="ajax"> 
    <a href="" onclick="return refreshDiv('ajax');">CLICK HERE!</a>

    <?php trigger here>
    <script type="text/javascript" language="javascript">
         Event.observe(window, 'load', function() { release(); });
    </script>
    <? php trigger end>
</div>

I must put it in the DIV section which is getting reloaded, cause there is a trigger from PHP which decides to output the function release(); or not.

Can anybody help here?


Solution

  • Obviously the window is already loaded by the time your AJAX executes so there is no window load event. If you know this then the HTML part looks much simpler:

    <div id="ajax"> 
        <a href="" onclick="return refreshDiv('ajax');">CLICK HERE!</a>
    
        <?php trigger here ?>
        <script type="text/javascript" language="javascript">release();</script>
        <?php trigger end ?>
    </div>
    

    You might need to check the page request really is AJAX, in which case it must be part of the PHP trigger.


    PS. function() { release(); } is just a longer way of saying release - you could have used that originally.