I have used ajax (not jQuery, simple JavaScript) to call a php file that 'includes' another php file, which has some javascript code in it as well (there's a script block, to be specific).
Code: Javascript: ( x
is a parameter to the function that has this code)
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState === 4 && xmlhttp.status === 200)
{
document.getElementById('content').innerHTML = xmlhttp.responseText;
}
};
xmlhttp.open("POST", "../include/ondemand.php");
xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xmlhttp.send("f=load_page&page=" + x.name);
ondemand.php:
case 'load_page':
$path = '../include/' . $_REQUEST['page'] .'.php';
include $path;
The file is included just fine. The PHP is processed as expected.
If (say) the included file has an element with the id 'any_random_id'
, the javascript code document.getElementById
would work for that element and be able to detect it. But say it has a function called ' do_something()
' - if I try to call that function, nothing happens. It's like it's not there.
I have tried using eval()
on the script block (by giving it an id) - in that case, the javascript executes properly, but the elements that have (say) the attribute "onchange='do_something()'"
don't really do anything on change.
Is there a solution? If not, any other approach?
UPDATE: Found the solution: iFrames . Using AJAX, I call a php script to load the particular page's (with required prefixes, blah) and then add that to the src of an iFrame. That does the job. Thanks for the help :)
It is technically possible to parse the AJAX results into an object using .responseXML
then you can run the results of that through a function to create the elements using document.createElement()
. Event-related attributes will obviously need to be given their own functions, but you can eval()
the contents of the event-related attributes as-well as the contents of the script elements. You need to be absolutely certain that the AJAX response is 100% XML-standards-compliant or it will not work.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.