Search code examples
javascriptajaxpageload

Doesn't work executing javascript script after ajax-loaded a page


I have a page which does an AJAX call and loads an entire page. The page that gets loaded has some Javascript. The javascript works on page by itself when loaded, but when its gets loaded by AJAX, the Javascript does not work. I dont know what I am missing.


Solution

  • This sounds like it has something to do with the asynchronous property of AJAX. When your page loads, the Javascript is executed immediately on the then-available content. Because AJAX calls happen AFTER your page is loaded (it is not a part of loading the page, it happens asynchronously) the Javascript is not applied to the result returning from the AJAX-calls. This is simply because the objects that are created or updated by the AJAX calls don't exist yet when the original Javascript is executed.

    This is how AJAX is designed and meant to be. You could solve it by adding some Javascript to the result of the AJAX call, to make sure that the necessary procedures are executed again after the result of the AJAX call has been processed.

    Without showing any of your code or a more detailed description, there's not much more to say about this problem.