Hi Michael,

The solution is simpler than you think.

All you have to do when you load an HTML document that contains a
script, is to think of it as being automatically injected into your
jQuery namespace ($). This means that you no longer have to do
another .ready() or jQuery(callback) inside your AJAX'd HTML, but
simply write your script as though it were inside .ready() or
jQuery(callback).

In other words, let's assume you want to get file.html that has a
script tag such as:

<script>
 $(document).ready(function() {
     alert('Dynamically injected code executed and I want to display '
+ showHTML());

     function showHTML() {
        $("b").html()
     }
  });
</script>
<body>
  <b>My HTML</b>
</body>

Normally, this would work if it was file.html was called directly.
What happens when you want to call file.html via jQuery's AJAX
function? Nothing. It just ignores it silently.

So basically all you have to do is remove the ready(function) since
you no longer need to worry about namespacing. As I said earlier, just
think of it as your dynamically injected <script> as already being
part of jQuery's namespace by the time the AJAX is finished.

The new file.html would thus simply be:

<script>
     alert('Dynamically injected code executed and I want to display '
+ showHTML());

     function showHTML() {
        $("b").html()
     }
</script>
<body>
  <b>My HTML</b>
</body>

This should work for you. Note that I haven't tested the dummy code
above, but that's the general idea.

Let me know if it works for you!

Milan Adamovsky
http://www.perlscript.com


On Aug 18, 10:42 am, Michael Anckaert <[EMAIL PROTECTED]>
wrote:
> Hello Karl,
>
> Thank you for your reply. But I think you mis understand me.
> I want the scripts in the page I loaded with ajax to execute, not let
> my already existing scripts work with the newly loaded content.
>
> For example:
> main.html:
>   function test() { ... }
>
> newpage.html:
>  onready function
>  other function
>
> So when I load the newpage.html into a div in  main.html, I want to
> execute the onready function from newpage.html
>
> On Aug 18, 4:04 pm, Karl Swedberg <[EMAIL PROTECTED]> wrote:
>
>
>
> > Hi Michael,
>
> > You have run into a fairly common issue: how to get events to work  
> > with elements that are added to the DOM, through either ajax or simple  
> > DOM mainpulation, after the "document ready" code has already fired.
>
> > This FAQ topic should answer your question:
>
> >http://docs.jquery.com/Frequently_Asked_Questions#Why_do_my_events_st...
>
> > If you still have problems after reading through it and trying one of  
> > the many solutions, let us know.
>
> > --Karl- Hide quoted text -
>
> - Show quoted text -

Reply via email to