在阿贾克斯HTML效应初探asp.net的eval脚本 [英] asp.net eval script in ajax html reponse

查看:180
本文介绍了在阿贾克斯HTML效应初探asp.net的eval脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的更新面板,我的回答有一些JavaScript一样吼叫。一个成功的响应后,我需要的eval它,加载(与外部脚本)

例如:我的HTML响应

 <脚本类型=文/ JavaScript的SRC =test.js>< / SCRIPT>
<脚本类型=文/ JavaScript的>
警报(ASD);
< / SCRIPT>
< D​​IV>测试< / DIV>
< D​​IV>等等等等< / DIV>
 

解决方案

我不知道这个问题是否仍然对你很重要,但是我会尽量提供低于一个合理的答案。

AJAX框架不评估其通过的UpdatePanel调用返回的脚本。你必须重新连接外部脚本文件,从而使浏览器可以要求他们和评估所有内嵌脚本。您可以使用一个小模块下面我有贴。

  VAR UpdatePanelEnhancer =功能()
{
    VAR divSelector ='#liveArea';
    功能evaluateScripts()
    {
        $(divSelector).find('脚本')。每个(函数()
        {
            如果(this.src)
            {
                $('身体')追加(本);
            }
            其他
            {
                VAR含量= $(本)。html的();
                的eval(内容);
            }
        });
    };

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

它的薄弱部分是,你必须要对其中module应该寻找一个脚本来评估(例如,在liveArea),虽然可以扩展模块,并提供一些cinfiguration它的元素提供了一个选择。另外,我强烈建议你之前加载外部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)

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 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);
} ();

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 .

这篇关于在阿贾克斯HTML效应初探asp.net的eval脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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