Results 1 to 2 of 2

Thread: Ajax Not Loading jQuery Scripts On Main Page

  1. #1
    Join Date
    Jul 2011
    Posts
    56
    Thanks
    5
    Thanked 1 Time in 1 Post

    Default Ajax Not Loading jQuery Scripts On Main Page

    Hi!

    On my page, a user will click on an image which sends data to an ajax script which in turn sends a variable to a PHP script on another page. Ajax gets the response and replaces a DIV's content with that PHP's page's content. It will work except for the fact that jQuery won't load for that PHP page.

    JS Function:
    Code:
    function wearitem(name) {
    if (window.XMLHttpRequest)
      {// code for IE7+, Firefox, Chrome, Opera, Safari
      xmlhttp=new XMLHttpRequest();
      }
    else
      {// code for IE6, IE5
      xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
      }
    xmlhttp.onreadystatechange=function()
      {
      if (xmlhttp.readyState==4 && xmlhttp.status==200)
        {
    		var response=xmlhttp.responseText;
    		
        document.getElementById("loadoutdisplay").innerHTML=response;
    	}
      }
    xmlhttp.open("POST","loadout.php?wear="+ name,true);
    xmlhttp.send();
    }
    The PHP page is just pure PHP and HTML, there isn't any Javascript. All the JS stuff is on the page of the JS function above.

    Thanks in advance and let me know if you need any more info.

    -M2com

  2. The Following User Says Thank You to M2com For This Useful Post:

    vanchuyenhang (07-24-2014)

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

    Default

    Many scripts initialize onload, or at document ready. Either way, that's over by the time this code:

    Code:
      if (xmlhttp.readyState==4 && xmlhttp.status==200)
        {
    		var response=xmlhttp.responseText;
    		
        document.getElementById("loadoutdisplay").innerHTML=response;
    	}
    executes. If you need to initialize the new content, this code is also where you would do that:

    Code:
      if (xmlhttp.readyState==4 && xmlhttp.status==200)
        {
    		var response=xmlhttp.responseText;
    		
        document.getElementById("loadoutdisplay").innerHTML=response;
        // initialize the new content here
    	}
    but it can be complicated. For instance different content might be returned at different times and therefore perhaps need different treatment. And/or the script who's initialization routine you wish to run at that point might not be able to do so for one reason or another.

    Using jQuery ajax() and/or live() (for older versions) or on() (for jQuery 1.8+) can often simplify some of this.

    If you want more help, please provide a link to the page on your site that shows the problem(s).
    Last edited by jscheuer1; 07-23-2014 at 08:40 PM. Reason: add info
    - John
    ________________________

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

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

    M2com (07-29-2014)

Similar Threads

  1. onfocus Loading Page jquery
    By davelf in forum JavaScript
    Replies: 8
    Last Post: 11-06-2011, 04:11 PM
  2. Replies: 0
    Last Post: 01-20-2011, 04:24 AM
  3. Images not loading when returning to main page
    By sbutler in forum Dynamic Drive scripts help
    Replies: 2
    Last Post: 03-22-2010, 08:59 PM
  4. Dynamic Ajax Content not loading jquery on pages being loaded
    By cmccarra in forum Dynamic Drive scripts help
    Replies: 0
    Last Post: 03-21-2010, 01:42 PM
  5. jQuery TreeView menu problem, expands first node on when loading page
    By Windwill in forum Dynamic Drive scripts help
    Replies: 0
    Last Post: 09-03-2009, 08:02 AM

Tags for this Thread

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
  •