Execute function in html loaded with ajax

If I load an html document into a div tag, for example, is there a way to call a function that I know exists in the loaded html?

Something like that:

$('#foo').load('bar.html', function(){ //call a function in bar.html }); 
+4
source share
1 answer

You put your JS code in a separate file (you load it into the downloaded html file) and use getScript as follows:

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

So, create a separate JS file (it is recommended not to mix it with an html or html document) and call it inside your bar.html file:

 <script href="js/fileName.js"></script> 

With this, you can also get this file to work with downloads using getScript .

+5
source

Source: https://habr.com/ru/post/1391801/


All Articles