tags:

views:

98

answers:

3

I've been using jQuery .load() to load content into a div. The content being .loaded occasionally has a document.ready() function which is called, and works, correctly (i.e. the ready() function is called). However, when I use an element ID in the .load(), such as: .load ("test.php #content"), the Javascript is no longer executed even if I put the script tag inside of the element that is being loaded. Does anyone have a solution to this other than to not use the element ID in the .load()? Thanks in advance.

Here is the dynamic content (loadDialogTest.php):

    <div id="test">
        <div>
         Hello, World!
        </div>


        <script type="text/javascript">
            $(document).ready (function () {
                alert ("ready");
            });
        </script>
    </div>

and here is the page (where the element ID is NOT specified) that loads it (shortened as much as possible while maintaining the form:

    <head>
        <script type="text/javascript">
            $(document).ready (function () {
                $("#openDialog").click (function () {
                    $("<div></div>")
                        .load ("loadDialogTest.php")
                        .appendTo ($("#containingDiv"))
                        .dialog ({
                        autoOpen: 'false',
                        title: 'Test This!',
                        close: function () {
                            $(this).dialog ('destroy').remove ();
                        }
                    });
                });
            });
        </script>
    </head>

    <body>
        <a href="#" id="openDialog">Open it</a>

        <div id="containingDiv">
        </div>
    </body>

If I put #test after the .php file as the div to load, the jQuery ready() function is no longer called.

A: 

Could you not have the JavaScript on the page that loads it rather than the page that is being loaded.

I'm not sure why the javascript is not being run but I get the same problem.

Pez Cuckow
Technically, yes. However, I'm a huge proponent of encapsulating the dynamic page's functionality in the page itself. Once you start delegating functionality to the calling page then one of the main reasons for loading it dynamically is gone. I have "one" method I've come up with but I was hoping there was a more elegant solution. The current method I came up with is to pass a parameter in the query string that I check for in PHP. If I don't see that parameter, I have a huge "if" statement around the block of code that should not be sent back, but again, it's "kind of" a hack.
Dave
A: 

I read somewhere that you have to .remove() the script tags first, append the loaded HTML to your DOM, then add the removed scripts. I'll update once I find the link.

UPDATE

Couldn't find the link, but the code you provided works fine for me. I'm using Jquery 1.4.2 and Jquery UI 1.8.1. I pasted your code into my site and it works, alerts "ready".

UPDATE 2

Ok, I've created a test page using your code, it seems to work ok - Load test page. The only thing you'll notice is that because its a CMS it redirects you shortly after loading the external content. But the Alert box does fire.

UPDATE 3

Dave, check out my test page now. I've added a function to the end of the load() that pulls the scripts from the loaded html and adds them to the body of the page. This appears to be working, but I did have to pull the Alert out of the document ready() function.

shanabus
Yes, it does work, but you did not change the opened page to include the element ID. In my example, the new .load would be: .load ("loadDialogTest.php #test")I realize the non-element specified version works, it's only when the element ID that contains the content to actually load is specified that it fails.
Dave
Oh i see, i've added .find("div#test") and it appears to be only loading that div now.
shanabus
I noticed your page displays the "ready" alert though you said you removed that from the loaded page. I tried the code, but the scripts never get appended to the body for some reason. I also used Chrome's DOM inspector to look for the dynamically loaded script to no avail. However, I'm going to tinker around with what you posted to see if I can make it work. Where did you put the .find ("div#test"), I didn't see that.
Dave
to clarify, I removed the ready() function, not the Alert("ready") dialog. The ready() function doesn't fire using the solution i posted.
shanabus
Ah, gotcha. For some reason, the scripts never get executed in my system. I used Chrome's DOM inspector and they never get appended to the body, though they are in the actual HTML that gets passed back to the .load() function. I'm going to try to use DOM inspector on your sample to see if I can figure out why that might be. However, the problem that I see I'm going to probably have is that calling this dialog multiple times would likely continue inserting unnecessary Javascript into the body each time it gets loaded.
Dave
well for that matter, is there a reason you don't add the necessary scripts to the calling page and then just execute those functions upon completion of the load()?
shanabus
Right. I want to avoid that if at all possible. I tend to like to keep the functionality of pages completely encapsulated within that page vs. putting dependent code in another page. This probably comes from doing 15+ years of object oriented programming :D. The functionality for this code is something similar to the Eclipse Preferences Window. There are two panels with a vertical divider that is movable. The left panel contains the preferences categories and the right panel contains the particular settings for the category. [see next comment]
Dave
I store each configuration setting option in a single PHP script and have a database table that lists each of these scripts as how to compose the settings window. When the settings window loads, it reads the table, and then iterates over the script names adding each one to a hidden div and adds the category name to the left panel. When the user clicks the setting in the left hand panel, the appropriate DIV is shown, etc. It's been working VERY well, however, there are a few things that are starting to "litter" the DOM and I'm trying to figure out how NOT to do that (i.e. keep the DOM clean)
Dave
Another reason why I like keeping each configuration category/item in a single script is that I can develop, and test, each item in isolation of the dialog and other configuration pages. If I need someone else to work on other configuration settings for the system, they too can be developed in complete isolation and then I just add a single row to the database table to insert the page into the configuration settings dialog.
Dave
A: 

This seems to be a known issue with .load as I ran into the same problem. If you check out the comments on the jQuery .load page you will find many others in the same boat, including me.

I actually have yet to find a solution I like, cause I quite like pulling out just an element of the page and like keeping files separate like you.

Given a few months have passed, have you come up with anything?

bladmiral
Actually, I think I did but I've put that project on the back burner at the moment. I haven't looked at the code in a while but if, and when, I do, I'll try to post something back here.
Dave
Awesome, thanks.
bladmiral