Page 1 of 1

jquery.js etc not included in php file called through ajax

Posted: Tue Jan 27, 2009 5:21 am
by saurabhprk
I have included some jquery libraries like jquery.js, thickbox.js and CSS files in my main php file i.e. main.php and they are included properly and all jquery code is working fine in all related pages/files.
As per my requirement, I am calling ajax.php to show some content on right column of main.php file though ajax(without reloading the page). The right column content is also coming properly.
The ISSUE is : When I am using the same jquery code in this ajaxified content, it does not work. I have tested several jquery codes that I have tested in other pages but no output through this ajax content.
Now, one thing that I noticed is that this ajax.php file is not in the same directory as main.php. Also, I cannot move that to same directory due to some reasons. So, I thought that may be the relative file paths defined in <head> to include jquery and css files may be the issue. Just to test, I replaced relative paths with absolute paths but still no library was able to work with this ajax content. I have tested a lot from my side but still not understand the reason/s behind this problem.
Could anyone please suggest me the missing part or mistake/s that I am doing while performing the above process?

Re: jquery.js etc not included in php file called through ajax

Posted: Tue Jan 27, 2009 10:25 am
by pickle
Content loaded through an AJAX request is not part of the DOM when the page first loads (obviously). When you call

Code: Select all

$(document).ready(function(){...
that tells jQuery that when the page is initially loaded, parse through the DOM and attach the actions you've specificed. When you make an AJAX request, it changes the DOM, but doesn't re-attach all the actions & listeners (because essentially you haven't told it to).

You have two options: use the new .live() function in jQuery 1.3.1, or put the code that attaches the listeners in a function, calling that function once when the document is loaded, and again after every AJAX request is complete.

Re: jquery.js etc not included in php file called through ajax

Posted: Sat Feb 07, 2009 6:17 am
by saurabhprk
Thanks for providing me some options. Could you elaborate the code or provide me some links that help me out to put proper code to attach the listeners and calling them when document is loaded again after every AJAX request?

Re: jquery.js etc not included in php file called through ajax

Posted: Mon Feb 09, 2009 9:50 am
by pickle
No - that can be researched yourself. Check the jQuery documentation - they usually have examples for the functions.