treehouse : what would you like to learn today?
Web Design Web Development iOS Development

Getting AJAX to display javascript

  • Following this tutorial javascript doesnt work with AJAX generated content.

    How would I go about getting javascript to work with AJAX displayed content based on that tutorial?

    Many thanks.
  • I have the same question -- for content created with an AJAX call, where do you place the script so that it affects the content or do you use .live() or .delegate()?
  • This seems to be a complex problem, as no one has given an answer as of yet. I so wish the tutorial I linked to had incorporated getting javascript to work.
  • I think this might be what you're after
    http://api.jquery.com/jQuery.getScript/
  • Ok I managed to successfully load a test script by using this code on the page.


    $.getScript("js/test.js", function (xhr) {
    try {
    CAGinit();
    } catch(err) {
    eval(xhr);
    CAGinit();
    }
    });

    Here is the content of that test script

    $(".result").html("<p>It Works!</p>");

    I have placed


    <div class="result"></div>

    inside the guts div.
    This code works when I go to the specific page url. However whenever I click using the navigation and the same page is generated by ajax, the script does not load.

    I take it I must put the $getscript somewhere inside the dynamicpage.js so it is passed onto the ajax loaded content?

    Very confused and would love to know how to get this working. I have searched for days for tutorials and am having no luck.
  • You'd need to run the getscript along with the function that changes the page, so it loads the correct one with the page loaded
  • Ah I think I got it working now. Now that I have got that simple test script working. How would I go about loading multiple scripts?

    Here is the code for my single test script.


    $(function() {

    var newHash = "",
    $mainContent = $("#main-content"),
    $pageWrap = $("#container"),
    baseHeight = 0,
    $el;

    $pageWrap.height($pageWrap.height());
    baseHeight = $pageWrap.height() - $mainContent.height();

    $("#projectimages").delegate("a", "click", function() {
    window.location.hash = $(this).attr("href");
    return false;
    });



    $(window).bind('hashchange', function(){

    newHash = window.location.hash.substring(1);

    if (newHash) {
    $mainContent
    .find("#guts")
    .fadeOut(200, function() {
    $mainContent.hide().load(newHash + " #guts", function() {
    $mainContent.fadeIn(900, function() {
    $pageWrap.animate({
    height: baseHeight + $mainContent.height() + "px"
    });
    });
    //get script start
    $.getScript("js/test.js", function (xhr) {
    try {
    CAGinit();
    } catch(err) {
    eval(xhr);
    CAGinit();
    }
    });
    //get script end
    $("#projectimages a").removeClass("current");
    $("#projectimages a[href="+newHash+"]").addClass("current");
    });
    });
    };
    });
    $(window).trigger('hashchange');
    });

  • why don't you put the javascript you're loading into a function in the main js file, then call that function after the page has loaded, this would be a performance gain too as it would only need to be parsed once
  • Ok I am still struggling to load two scrips. Basically I have script.js, and cycleall.js

    I have 2 images on the page, and they are going to be a slideshow which fades between the two images. I need the script.js which hides the 1 slide and cycleall.js which will do the fade transitions.

    I thought it would be a simple matter of just adding two get scripts with the names of these scripts and it would work.

    You can see that it does work on projects2.php however the generated ajax page it doesnt work. This puzzles me as my test script worked on the ajax generated page.