调用从 Ajax 响应返回的 JavaScript 函数 [英] Calling a JavaScript function returned from an Ajax response

查看:25
本文介绍了调用从 Ajax 响应返回的 JavaScript 函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个发送 Ajax 命令的系统,该命令返回一个包含函数的脚本块.将此数据正确插入 DIV 后,我希望能够调用此函数来执行所需的操作.

这可能吗?

解决方案

我认为在这种形式下正确解释您的问题:好吧,我已经完成了所有的 Ajax 内容;我只是想知道 JavaScript从那一刻起,我插入到 DIV 中的 Ajax 回调函数可以随时调用,也就是说,我不想在上下文中调用它到回调返回".

好的,如果您的意思是这样,那么答案是肯定的,您可以在浏览器中页面持久化期间的任何时刻调用您的新代码,条件如下:

1) 你的 Ajax 回调返回的 JavaScript 代码必须在语法上没问题;
2) 即使您的函数声明被插入到现有

元素内的

我没有使用 Ajax,但概念是一样的(即使我选择的例子肯定不是很聪明:-)

一般来说,我不质疑你的解决方案设计,即在单独的.js文件中外部化+泛化功能是否或多或少合适,但请注意,这样的解决方案可能会引发更多问题,特别是如果您的 Ajax 调用应该重复,即如果同一个函数的上下文应该改变或者如果声明的函数持久性应该受到关注,那么也许您应该认真考虑将您的设计更改为该线程中建议的示例之一.

最后,如果我误解了您的问题,并且您正在谈论当您的 Ajax 回调返回时函数的上下文调用,那么我的感觉是建议使用 Prototype 方法 由 krosenvold 描述,因为它是跨浏览器的,经过测试并且功能齐全,这可以为您提供更好的未来实施路线图.

I have a system where I send an Ajax command, which returns a script block with a function in it. After this data is correctly inserted in the DIV, I want to be able to call this function to perform the required actions.

Is this possible?

解决方案

I think to correctly interpret your question under this form: "OK, I'm already done with all the Ajax stuff; I just wish to know if the JavaScript function my Ajax callback inserted into the DIV is callable at any time from that moment on, that is, I do not want to call it contextually to the callback return".

OK, if you mean something like this the answer is yes, you can invoke your new code by that moment at any time during the page persistence within the browser, under the following conditions:

1) Your JavaScript code returned by Ajax callback must be syntactically OK;
2) Even if your function declaration is inserted into a <script> block within an existing <div> element, the browser won't know the new function exists, as the declaration code has never been executed. So, you must eval() your declaration code returned by the Ajax callback, in order to effectively declare your new function and have it available during the whole page lifetime.

Even if quite dummy, this code explains the idea:

<html>
    <body>
        <div id="div1">
        </div>
        <div id="div2">
            <input type="button" value="Go!" onclick="go()" />
        </div>
        <script type="text/javascript">
            var newsc = '<script id="sc1" type="text/javascript">function go() { alert("GO!") }</script>';
            var e = document.getElementById('div1');
            e.innerHTML = newsc;
            eval(document.getElementById('sc1').innerHTML);
        </script>
    </body>
</html>

I didn't use Ajax, but the concept is the same (even if the example I chose sure isn't much smart :-)

Generally speaking, I do not question your solution design, i.e. whether it is more or less appropriate to externalize + generalize the function in a separate .js file and the like, but please take note that such a solution could raise further problems, especially if your Ajax invocations should repeat, i.e. if the context of the same function should change or in case the declared function persistence should be concerned, so maybe you should seriously consider to change your design to one of the suggested examples in this thread.

Finally, if I misunderstood your question, and you're talking about contextual invocation of the function when your Ajax callback returns, then my feeling is to suggest the Prototype approach described by krosenvold, as it is cross-browser, tested and fully functional, and this can give you a better roadmap for future implementations.

这篇关于调用从 Ajax 响应返回的 JavaScript 函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆