views:

350

answers:

3

I loaded a second.php file in first.php using jquery ajax load function

$(".divclass").click(function(){ $("#divid").load('second.php', { id : $(this).attr('id') }); // alert("view Clicked"); });

the second.php loaded second php content in first.php file success.

but second.php contains some jquery function... in second.php

$(document).ready(function(){ alert("second document jquery"); });

this not working... and Back this back javascript also not working when load in first.php but both working when directly calling second.php

what to do

+1  A: 
Pointy
im doing add, edit, update prog.first i display the list in a tablewhn i click on a particular name in list it loads the person details in the body content without reloading the pageso the second page contains view on particular id(row)then come to edit part when click on edit option in first.php(default list page)it loads d edit php in the body content..whn clicking update it executes another jquery ajax funtion to post data to update(i.e update.php)my problem is the jquery in edit page to post update not working..not only the update, any jquery in loaded php is nt working..
udhaya
AJAX content loaded using `$.get` and `$.post` (also `$.ajax`) will get the "ready" event. `load` in other hand will strip out any javascript code, so no code from loaded file will be executed. See my answers for the workaround.
Donny Kurnia
A: 

The javascript in second.php won't run like that, why don't you just use the success function?

$(".divclass").click(function(){
  $('#divid').load('second.php', { id : $(this).attr('id') }, function() {
    alert('second document jquery');
  });
});
fire
there is some dynamic operations in second.php which is loaded in first.phplike click on any particular div in second.php
udhaya
Ok take a look at getScript() - http://api.jquery.com/jQuery.getScript/
fire
A: 

Use $.get instead of load:

$(".divclass").click(function(){
  var elmt_id = $(this).attr('id');
  $.get('second.php',
        { id: elmt_id, nbRandom: Math.random() },
        function(data){
          $("#divid").html(data);
        });
});

Why? Because load will strip out any javascript from loaded page. $.get will put all content from second.php, any js code there will be executed.

I use the code like above in all my project. The page that loaded have jQuery DOM ready block code, and all worked.

In my code above, I add second parameter. The purpose is just to prevent caching in IE. Choose a name that will not be used by second.php.

Donny Kurnia