views:

60

answers:

3

A friend and I are looking over a website involving a button that makes an AJAX call that upon successful return, loads HTML into a specified DIV tag and calls a Javascript function. The problem is that it will only work twice. After clicking it twice it fails to work properly. We are trying to dynamically control what the ready() invoked function is.

here is a simple case:

<html>
<head>
<script type="text/javascript" src="jquery-1.4.1.js"> </script>
<script type="text/javascript">
$(function(){
  $("#trigger").click(
    function(){
     $("#target").load('load.html');
    }
  );
});
</script>
</head>
<body>

<div id="target" style='width:200px;height:200px;border: 1px solid black'>
Not Loaded
</div>
<input type="button" value="Load AJAX" id="trigger" />
</body>
</html>

load.html contains:

<script type="text/javascript">
$(function(){
  alert('loaded stuff');
});
</script>
loaded

Here is the sample: http://phoenixillusion.net/demos/ajax/

Thanks! EDIT Forgot to put information about the contents of load.html

A: 

It actually is making the call to load each time although it does not appear to be since the popup does not come up after 2 clicks.

Use a console like chrome or firebug and watch the XHR on each click you will see it makes a call each click.

Chris
Right, so do you know why $("#target").load('load.html'); isn't being called?
KennyCason
It is being called, each and everytime as I said above. See screen shot below, each load.html on there is from a click on your page. Which is what .load() is doing in your code. http://j.imagehost.org/0628/image.png
Chris
I'm sorry for the confusion, I messed up the wording. We knew the .load() is working properly, but are curious why the JS function in load.html isn't being called.
KennyCason
+1  A: 

You should upgrade to jQuery 1.4.2 which included a pretty heavy re-write of the events module, you can find full release notes here.

Nick Craver
Thanks! that was the problem
KennyCason
A: 

Try putting the javascript code in the main program instead:

<script type="text/javascript">
$(function(){
  $("#trigger").click(
    function() {
      $("#target").load('load.html', function() {
        alert('Loaded');
      });
    }
  );
});
</script>
Kranu
Thanks, The problem is that in our website one Javascript function makes an API call to our server via .post(), then it returns and calls other JS handler functions. Thanks though, The problem turned out to be the JQuery version, there is a bug with version 1.4.1
KennyCason