0

I have a main (index) page which loads pages dynamically and places them inside it's div but the Javascript within those pages doesn't execute. Specifically this part

<script type="text/javascript">
    $(document).ready(function(){

        $('#regForm').submit(function(e) {
            register();
            e.preventDefault(); 
        }); 
    });
</script>
Madmadmax
  • 79
  • 3
  • 10

3 Answers3

2

You will have to use getScript like this:

$('#foo').load('bar.html', function(){
    $.getScript("js/fileName.js");
    // call a function from fileName.js
});

You will have to put your JS code in that file and call that via getScript and then you can call functions from it as shown above.

Sarfraz
  • 377,238
  • 77
  • 533
  • 578
2

Write your javascript in the index.php or write at the bottom of loaded page without document.ready

jumancy
  • 1,290
  • 1
  • 9
  • 16
1

This is in reality a cross-browser issue: When <div>s are dynamically filled with HTML containing <script> tags, these scripts may or may not run - and this behaviour is different not only between browsers, but also between browser versions.

The only workaround I know of is to extract your JS, send it seperately and execute it after the <div> content has been set.

Eugen Rieck
  • 64,175
  • 10
  • 70
  • 92