0

i have a page that uses JQuery.load() to load main-panel's content.

<body>
    <div id="main-panel">
        // load content dynamically
    </div>

<script src="myscripts.js"></script>
</body>

problem is, when the content arrives, it seems like it is not aware that there are functionalities inside myscripts..

so my work around is to reload again myscripts.js, and then all works fine.

jQuery.getScript('myscripts.js');

but the downside is, myscripts.js will be loaded multiple times.. how can i make ajax result content to be aware of my host page's script?

Thank you in advance.

Lelio Faieta
  • 6,457
  • 7
  • 40
  • 74
tuty_fruity
  • 523
  • 1
  • 5
  • 18
  • you are loading dinamically some elements that are not in the DOM at load. If you are using jquery you can use [.on](http://api.jquery.com/on/). With plain js [addEventListener](https://stackoverflow.com/questions/30880757/javascript-equivalent-to-on) to target events to these elements – Lelio Faieta May 30 '18 at 09:43
  • try delegate event to element. ref:http://api.jquery.com/delegate/ Description: Attach a handler to one or more events for all elements that match the selector, now or in the future, based on a specific set of root elements. – jupeter May 30 '18 at 10:30

1 Answers1

0

Put your script before load content.

<script src="myscripts.js"></script>
<body>
    <div id="main-panel">
        // load content dynamically
    </div>

</body>
Vladimir.V.Bvn
  • 1,050
  • 1
  • 13
  • 13