Results 1 to 3 of 3

Thread: Javascript not executing in dynamically created DIV (using PHP)

  1. #1
    Join Date
    Aug 2008
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Question Javascript not executing in dynamically created DIV (using PHP)

    I have an onclick event that calls a Javascript function with calls a PHP page. The PHP page dynamically creates the content in a DIV the original onclick event triggered. Inside this dynamically created DIV a have another Javascript function, but it does no execute as I would expect.

    HTML
    Code:
    echo "<div onClick=\"ajaxpage('moreContent.php?id=$row[id]', div$row[id]');\">";
    Javascript
    Code:
    function ajaxpage(url, containerId)
    {
        var page_request = false
        if (window.XMLHttpRequest) // if Mozilla, Safari etc
        page_request = new XMLHttpRequest()
        else if (window.ActiveXObject){ // if IE
        try {
        page_request = new ActiveXObject("Msxml2.XMLHTTP")
        } 
        catch (e){
        try{
        page_request = new ActiveXObject("Microsoft.XMLHTTP")
        }
        catch (e){}
        }
        }
        else
        return false
        page_request.onreadystatechange=function()
        {
            loadpage(page_request, containerId)
        }
        page_request.open('GET', url, true)
        page_request.send(null)
        
        var dv = document.getElementById(containerId);
        dv.style.display = (dv.style.display == 'block' ? 'none' : 'block');
    }
    Inside the PHP the above Javascript calls (this bit doesn't load)...
    Code:
                    echo "<div class=\"userData\">";
                    echo "<table class=\"grid\" cellspacing=1 id=\"userGrid\">??</table>";
                    echo "<script type=\"text/javascript\">setup();</script>";
                    echo "</div>";
    How do I get this Javascript to load?

  2. #2
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    As you are trying to call a javascript function (setup), I assume that script is already loaded on the 'top' page. What you are then doing, importing the PHP page via AJAX is little different than importing an HTML page via AJAX. All the browser would see is:

    HTML Code:
    <div class="userData">
    <table class="grid" cellspacing=1 id="userGrid">??</table>
    <script type="text/javascript">setup();</script>
    </div>
    But the 'top' page is already loaded and parsed by the browser, so setup() doesn't run.

    This bit, from your AJAX routine:

    Code:
    loadpage(page_request, containerId)
    calls the loadpage function. Since you don't show that, I don't see how that works. Generally a function like that would test the readyState value of the page_request object. If it is 4, then it would set the innerHTML of the element with the id of containerId to the value of the page_request object's responseText property.

    In most cases, that would be the time to run your setup() function, something like so:

    Code:
    if(page_request.readyState == 4){
    document.getElementById(containerId).innerHTML = page_request.responseText;
    setup();
    }
    But, that will attempt to run setup() for every request that uses the main AJAX code. If that's not going to work out for other requests, you must determine a test to run at that point (before setup() is run) to determine whether or not to do that (run setup).

    Perhaps:

    Code:
    if(page_request.readyState == 4){
    document.getElementById(containerId).innerHTML = page_request.responseText;
    if(document.getElementById('userGrid'))
    setup();
    }
    Last edited by jscheuer1; 08-11-2008 at 05:41 PM. Reason: spelling
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  3. The Following User Says Thank You to jscheuer1 For This Useful Post:

    renphi (08-11-2008)

  4. #3
    Join Date
    Aug 2008
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default

    Thanks jscheuer1! Yes, the script is loaded at the top of my index.php page. Not sure if it's the best place, but add you suggestion as below and I now see the simple grid where I'd expect it.

    loadpage function
    Code:
    function loadpage(page_request, containerId)
    {
        if (page_request.readyState == 4 && (page_request.status==200 || window.location.href.indexOf("http")==-1))
        {
            document.getElementById(containerId).innerHTML=page_request.responseText
            setup();
        }
    }
    Just realized my redundancy there and added the setup(); to the original if block.
    Last edited by renphi; 08-11-2008 at 02:58 PM.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •