Steve
Steve

Reputation: 596

Loading pages into a div with JQuery, existing Javascript not working

I have a page I'm working on where a user clicks a link and it loads a new php file into an existing div. It works but the page that loads into the div will not function with existing Javascript stuff in the page.

I can include the <script type="text/javascript" src="js/admin.js"></script> into the loaded pages but when you flick back and forth between the pages I notice that RAM usage starts to go up and up, so I don't think this is the best way of doing it.

Any ideas how the loaded page can function with the already-loaded javascript from the index page?

Thanks!

Upvotes: 0

Views: 162

Answers (2)

Anthony Hatzopoulos
Anthony Hatzopoulos

Reputation: 10537

If you are using bind or click type events change to using something like on (or live or delegate if you are required to use jquery version less than 1.9)

OR/AND

In your function that loads in the page via ajax provide a call back that initiates only what is needed. Example:

$('#myDiv').load('ajax/page.php', function(){

    $('#myDiv a').customPlugin('whatever');
    $('#myDiv button').bind('click', function(){
        window.open('http://www.google.com/', 'some-window');
    });

});

Upvotes: 1

mikakun
mikakun

Reputation: 2265

bind your events like this :

 $(document).on({
           "event"  : function(e) {},
             ...

          }, "selector");

Upvotes: 1

Related Questions