[Solved] Javascript isn’t running in php function


Long shot on a wild guess here with the limited information you gave.

My assumption is that you are not “including” the file via PHP’s include, require, include_once or require_once functions, but are in fact using AJAX to load in the page’s content.

If this is the case, then I shall also assume you’re using innerHTML to put the content on the page.

Suddenly the solution is obvious: <script> tags added by innerHTML are not parsed and run. You could probably do something like this:

// assume `result` is the variable containing the AJAX response and `elem` the element it goes in
elem.innerHTML = result; // this doesn't change
result.match(/<script[^>]*>([\s\S]*?)<\/script>/i,function(m) {eval(m[1]);});

Please note however that eval should be avoided if possible. Consider redesigning your layout to use callbacks instead.

5

solved Javascript isn’t running in php function