简体   繁体   English

如何在从PHP回显页面获得的.post()输出数据中运行javascript函数

[英]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. 我通过调用x.php在网页运行时加载了新数据,我检索了动态生成的html和javascript。 But the javascripts functions did not run. 但是javascripts函数没有运行。

code in index page, call php page: 索引页面中的代码,调用php页面:

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

sample html 样本HTML

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

sample of output js functions: 输出 js函数示例:

<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? 如何在输出数据中与加载的早期js一起运行新的JavaScript函数? 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. 从Post / Get检索JS时,需要使用eval评估响应中不使用js的情况。 I mean the JS is not evaluated automatically, so that you need to perform manually 我的意思是JS不会自动评估,因此您需要手动执行

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

the output shall be pure js and additionally, is not secure use eval :) 输出应为纯js,此外, 使用eval是不安全的:)

Your returned javascript needs to be called by whatever javascript is there on the page ;) 您返回的javascript需要页面上所有的javascript调用;)

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: 当您将“ function”放在js之前时,就好像您在声明一个变量:

var x;

Also, your ids are mismatched currently (view vs view2), I suspect that may be just a typo though. 另外,您的ID当前不匹配(视图与视图2),我怀疑这可能只是拼写错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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