1

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

3 Answers 3

1

Your second file — which I presume is a fragment of HTML and not a complete document — is not going to get a "ready" event, because you're just modifying the DOM. If you need some embedded Javascript to run, just put it directly in a tag at the end of the fragment:

<div id='stuff'>
  <!-- ... --->
</div>
<script>
  alert("Hi I have just been loaded");
</script>

jQuery will make sure that the Javascript block runs when the fragment is added to the DOM.

Sign up to request clarification or add additional context in comments.

2 Comments

im doing add, edit, update prog. first i display the list in a table whn i click on a particular name in list it loads the person details in the body content without reloading the page so 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..
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.
0

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');
  });
});

1 Comment

there is some dynamic operations in second.php which is loaded in first.php like click on any particular div in second.php
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.