Search code examples
jqueryvariablesgetscopejquery-load

Accessing variable in HTML loaded with Jquery .load()


This probably has a simple answer, but I can't seem to find it anywhere. I'm loading a local HTML file into a modal overlay. The HTML file has a slideshow in it. I'd like to set a variable and access it in the loaded HTML to know which slide to start with.

I've tried sending it with the URL as a GET variable. I've tried sending it after the url in curly brackets and retrieving as POST. I've tried setting a global variable, but I'm fairly certain it's out of scope, and I can't seem to get it anyway.

Here is some relevant code:

$(document).ready(function() {

    $("a.slide0").click(function() {
        $( '#revealLayer' ).load( 'slideshow1.html?slide=0');
        $( '#revealLayer' ).appendTo( '#revealModal' );
        $( '#revealModal' ).reveal();
        return false;
    });

    $("a.slide1").click(function() {
        $( '#revealLayer' ).load( 'slideshow1.html?slide=1');
        $( '#revealLayer' ).appendTo( '#revealModal' );
        $( '#revealModal' ).reveal();
        return false;
    });

});

How can I get this on the receiving end? Help!


Solution

  • Already explained how to do this in this post here: problems with jquery load()

    Quick Mockup (Not Tested):

    This assumes that #revealModal is the div you want the information in, and that slideshow1.html?slide=[0-1] urls are actually linking to PHP files.

    $(document).ready(function() {
        $("a.slide0").click(function() {
            $('#revealModal').load('slideshow1.html', {slide:'0'}, function(){
                $(this).reveal();
                return false;
            });   
        });
    
        $("a.slide1").click(function() {
            $('#revealModal').load('slideshow1.html', {slide:'1'}, function(){
                $(this).reveal();
                return false;
            });
        });
    });
    

    The slideshow1.html?slide=1 and slideshow1.html?slide=0 will need to handle the $_POST['slide'] variable in PHP for this.