简体   繁体   中英

how to run the javascript function in the .post() output data recieved from a php echoed page

I loaded new data at my web page runtime by calling x.php I retrieve html and javascript dynamically generated. But the javascripts functions did not run.

code in index page, call php page:

$.post('x.php', {
    id: num
}, function (output) {
    document.getElementById('view').innerHTML = output;
});

sample html

<div id='view2'></div>

sample of output js functions:

<script type="text/javascript">
    function a(y, s) {
        id + y / s
    }

    function v(d) {
        document.getElementById('view2').innerHTML = d;
    }
</script>

how do I make the new JavaScript functions in the output data run along with earlier js loaded? other functions will depend on this new generated function

When you retrieve JS from a Post/Get, not using the js at the response needs to be evaluated using eval. I mean the JS is not evaluated automatically, so that you need to perform manually

$.post('x.php', {
    id: num
}, function (output) {
    eval(output);
}

the output shall be pure js and additionally, is not secure use eval :)

Your returned javascript needs to be called by whatever javascript is there on the page ;)

You might try putting:

a(y,s);
v(d);

after your function declarations up there. When you put "function" before the js it's like you're declaring a variable:

var x;

Also, your ids are mismatched currently (view vs view2), I suspect that may be just a typo though.

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM