views:

407

answers:

3

I have a page setup that uses jQuery + AJAX to load content into an accordion frame. It works fine for loading straight text/HTML but I now need to load Javascript along with the HTML.

To elaborate, I'm using GalleryView to display images in a photo gallery section of the accordion. When I load the HTML file, however, the scripts aren't being executed and I'm just seeing straight images. No gallery.

Is there a cleaner method of .load()'ing the HTML file then eval()'ing it? I've tried putting the GalleryView code into the main page (with the accordion) in the load() functions callback in hopes it could then initialize the GalleryView plugin, but no go.

+2  A: 

You can use the LiveQuery plugin. In your original or "master" file that the other content is being loaded into, you bind your code to those elements being loaded in. The plugin will detect when the content is loaded in and execute the code.

jQuery:

$(document).ready(function(){
    $('#gallery').livequery(function(){
        $(this).galleryView();
    });
});

The plugin is available: http://plugins.jquery.com/project/livequery/

a432511
It didn't seem to work. I have the following `$('.content').livequery(function() { $(this).load(function() { $('#gallery').galleryView( {` Just to verify that's correct, '.content' is the DIV the data is being loaded into (via ajax). '#gallery' is the ID of the UL being used by GalleryView.
panas
bind the livequery event to the #gallery id
a432511
refer to my edit
a432511
and you should not bind to the div that the content is loaded into, you must bind to a new element that is being pulled from the other page
a432511
Still doesn't seem to work. Nothing showed up in the content div and the page begun to lag :/
panas
hm... i use livequery quite heavily in a 100% ajax webapp and ive never had lag issues. Maybe try what the other guy suggested and make sure the JS is outside the head/inside the body of your new content. I would also recommend using Firefox with the Firebug plugin. Then you can use the console to find out what is really going on.
a432511
livequery just monitors for ajax and rebinds functions to elements. So, for example, you are loading in a new #gallery element... livequery sees that happen and rebinds your function to that element. Your livequery binding is happening in the "master" page i assume?
a432511
I posted edits to the link you provided
a432511
A: 

Load will automatically filter out all but the body tag in the document. If you have your javascript in the head of the document, it won't be loaded with the document. Try moving your javascript into the body tag and see if that works.

In jQuery 1.2 you can now specify a jQuery selector in the URL. Doing so will filter the incoming HTML document, only injecting the elements that match the selector. The syntax looks something like "url #some > selector". Default selector "body>*" always applies. If the URL contains a space it should be escape()d. See the examples for more information.

Updated based on comments

You'll need to load your CSS with your base page. Since the link tag must be in the head element, there isn't any (easy) way to load it via AJAX. You could add a link to the head of your document, but getting the href for the link would probably require a separate call or changing to return the link and html wrapped as a json object. Better to just include the CSS in the main document.

I think if you returned just the following content from your AJAX call it ought to work.

<ul id="gallery">
   <li><span class="panel-overlay">This is an overlay</span><img src="img1.jpg" alt="test1" title="test1" /></li>
   <li><span class="panel-overlay">This is an overlay</span><img src="img2.jpg" alt="test2" title="test2" /></li>
   <li><span class="panel-overlay">This is an overlay</span><img src="img3.jpg" alt="test3" title="test3" /></li>
   <li><span class="panel-overlay">This is an overlay</span><img src="img4.jpg" alt="test4" title="test4" /></li>
</ul>

<script type="text/javascript">
$(function() {
    $('#gallery').galleryView(
    {
        panel_width: 474,
        panel_height: 353,
        frame_width: 50,
        frame_height: 50,
        transition_speed: 350,
        easing: 'easeInOutQuad',
        transition_interval: 0
    });
});
</script>
tvanfosson
Here's the code: http://pastebin.com/m66533561
panas
two things.. you should include the livequery js file before your gallery js files. Also you are not binding to your #gallery element and you should not have a <head> or <body> tag in your file that gets loaded because it is being loaded into a page that already has that.
a432511
all you need is the <ul> // list elements </ul> in the file that is being loaded
a432511
I posted edits to the link you provided
a432511
I removed everything but the <ul> from the included file. Changed the livequery initializer to `$(document).ready(function() { $('#gallery').livequery(function() { $(this).galleryView( {`. The images are being included (the UL) but the CSS file doesn't seem to be taking effect on it and there's no gallery. So the livequery call doesn't seem to be working
panas
Did you add the $(this).load(function() { } ); ? I left that out of my edit up top by accident... If that doesnt work im out of ideas. Cant say i didnt try though. Like i said before... ive used livequery all over the place... it has never failed. there must be something else going on here
a432511
If you want to see if the function inside the livequery event is being called, add console.log("test"); inside the $(this).load(function() {}); Make sure you are using firefox/firebug and have the console open
a432511
Or if you prefer not to use firefox/firebug use alert("test");
a432511
Yeah, forgot the load() function. But added it in then and it still failed. So tried adding an alert() inside it and it didn't fire. Seems there's something wrong with my livequery() call :/
panas
The alert() fired after removing the load() function from within livequery. But the gallery still doesn't work. Just causes my browser to lag. Any more ideas? :/
panas
Firebug seems to be returning the content and loading it in the DOM, I just can't see it in the browser...
panas
Ok... i think i have the solution. Refer to my new answer that I posted...
a432511
+1  A: 

My new answer...

No livequery necessary. Your browser was lagging because for some reason that galleryView plugin and livequery were causing an infinite loop. Probably just some incompatibility there (I didn't look to deeply at that once i realized it was causing an infinite loop).

Use this:

$(document).ready(function(){

    $('#content').load('theOtherFile.html', function(){

        $(this).find('#gallery').galleryView(); 

    });

});

This takes advantage of the callback method that the .load() function allows you to implement. When the ajax call is complete and the content has been loaded, it will call the callback function. Then you can search "this", which is the #content div, for your now loaded #gallery UL. I also noticed that you had two $(document).ready(function(){}); uses. You should really only use one of those.

a432511
By the way I tested this locally and it works...
a432511