Cannot call user-defined JS function after returning AJAX contents

I had a problem calling a custom JS function after an AJAX call. Basically, I created a couple of radio buttons on the html main page. When the user clicks on one of the switches, he calls an AJAX call and returns another html file in the contents of the "div" that I set on the main html page. Another html file contains a user-defined JS function (for example, "updateContent ()"), which uses the onclick event handler to call the function. When I launch the application and press the button. I saw how firebug complained about the error "updateContent () is not defined". The function itself works great and needs to be correctly defined. Please help me about this! Thank.

+3
source share
2 answers

Google's magic words are "JSON-P" and " On Demand Javascript ." Basically, instead of loading a piece, you create and load a tag. Then javascript is interpreted, your function is determined, a nail is found, the horse is trained, and the kingdom is not lost.

+2
source

This is because the js code that you embed through innerHTML will never be executed. You may need to extract it from the HTML code by running it through eval. PrototypeJS has a configuration option to do this automatically.

I would recommend you highlight the js code and include it with the rest of the js code.

+1
source

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


All Articles