Ajax html响应中的asp.net评估脚本 [英] asp.net eval script in ajax html response

查看:49
本文介绍了Ajax html响应中的asp.net评估脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用更新面板,我的响应中包含一些JavaScript,例如波纹管.成功响应后,我需要评估它,并使用外部脚本加载它

I'm using update panel, my response have some javascript like bellow. After a success response, I need to eval it, load it (with external script)

例如:我的html响应

ex: my html response

<script type="text/javascript" src="test.js"></script>
<script type="text/javascript">
alert('asd');
</script>
<div>test</div>
<div>blah blah blah</div>

推荐答案

我不确定这个问题对您是否仍然很重要,但是我将在下面尝试提供一个合理的答案.

I'm not sure whether this question is still important for you, however I will try to provide a reasonable answer below.

AJAX框架不评估通过UpdatePanel调用返回的脚本.您必须重新附加外部脚本以进行文档记录,以便浏览器可以请求它们并评估所有内联脚本.您可以使用下面粘贴的一个小模块.

AJAX framework does not evaluate scripts which are returned via UpdatePanel calls. You have to re-attach external scripts to document, so that browser could request for them and evaluate all inline scripts. You can use a small module I have paste below.

var UpdatePanelEnhancer = function ()
{
    var divSelector = '#liveArea';
    function evaluateScripts()
    {
        $(divSelector).find('script').each(function ()
        {
            if (this.src)
            {
                $('body').append(this);
            }
            else
            {
                var content = $(this).html();
                eval(content);
            }
        });
    };

    Sys.Application.add_load(evaluateScripts);
} ();

它的弱点是您必须为元素提供一个选择器,模块应该在其中寻找脚本来评估(尽管示例中为"liveArea"),尽管您可以扩展模块并为其提供一些外观.另外,我强烈建议您之前加载外部javascript.如果由于某种原因不能执行此操作,则应另外检查脚本是否已被引用,以避免不必要的调用以及潜在的意外行为和错误.

The weak part of it is that you have to provide a selector for the element where module should look for a scripts to evaluate ('liveArea' in example), although you can extend the module and provide some cinfiguration to it. Also, I would strongly recommend you to load external javascripts before. If you cannot do it for some reason you should additionally check whether script is already referenced or not to avoid necessary calls and potentially unexpected behaviors and errors .

这篇关于Ajax html响应中的asp.net评估脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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