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

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

问题描述

我在哪里我送一个Ajax命令,该命令将返回所有在这一个功能的脚本块的系统。在此之后的数据是在DIV正确插入,我希望能够调用这个函数来执行所需操作。

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.

这可能吗?

推荐答案

我认为正确的跨preT根据此表你的问题:好吧,我已经与所有的Ajax的东西做的,我只是想知道如果JavaScript函数我的Ajax回调插入DIV是可调用的从这一刻起任何时候,就是我不想上下文称之为回调的回报。

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,如果你的意思是这样的答案是肯定的,你可以通过那一刻随时在浏览器中的页面持久性调用新的code,在下列条件下:

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)你的JavaScript $返回Ajax回调必须是语法行C $ C;
2)即使你的函数声明插入<脚本> 现有&LT中的块; DIV> 元素,浏览器将不知道新功能的存在,作为申报code从来没有被执行。所以,你必须的eval()由Ajax回调返回,为了有效地申报新功能,并具有在整个页面生命周期也可用你的报关code。

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.

即使很假,这code说明了想法:

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>

我没有使用Ajax,但概念是相同的(即使我选择了肯定的例子并不多聪明: - )

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

一般来说,我不怀疑你的解决方案设计,即无论是或多或少适当的外化+概括功能在一个单独的.js文件之类的,但是请注意,这样的解决方案可以提高更多的问题,特别是如果你的Ajax调用应该重复,也就是说,如果同样的功能的情况下应该改变或在申报功能的持久性应该关注的,所以也许你应该认真考虑您的设计更改为在此线程建议的例子之一。

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.

最后,如果我误解你的问题,你在谈论的上下文的函数调用,当你的Ajax回调的回报,那么我的感觉是建议的原型方法<一href="http://stackoverflow.com/questions/510779/calling-a-javascript-function-returned-from-a-ajax-response/510818#510818">described通过krosenvold ,因为它是跨浏览器,测试功能齐全,这可以给你一个更好的路线图,未来的实现。

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天全站免登陆