从Ajax请求执行内联脚本

execute inline script from Ajax request

本文关键字:脚本 执行 Ajax 请求      更新时间:2023-09-26

在我的应用程序主页上,我建立了一个" widgets "系统。小部件的源是在服务器端生成的,并通过Ajax推送到客户端。

Ext.Ajax.request({
    url: '/get-widgets',
    success: function( response ) {
        var boxes = Ext.decode( response.responseText );
        Ext.each( boxes, function( box ) {
            box.id = 'cell-' + box.cell;
            var element = Ext.DomHelper.append( 'canvas', {
                tag: 'div',
                id: box.id,
                cls: 'widget size-' + ( box.size || '11' ),
                children: [{
                    tag: 'div',
                    cls: 'title',
                    html: box.title
                }, {
                    tag: 'div',
                    cls: 'main',
                    html: box.body
                }]
            }, true );
        });
    }
});

问题是,一些小部件有一些内联的Javascript在他们的身体,需要执行。这在Firefox中可以很好地工作,但在Chrome中不行。

是否有一个标志或你需要激活内联代码被执行?

发现Ext.Element::update()方法具有一个用于扫描和执行内联脚本的标志。我将代码修改如下以利用此方法:

var element = Ext.DomHelper.append( 'canvas', {
    tag: 'div',
    id: box.id,
    cls: 'widget size-' + ( box.size || '11' ) + ' ' + ( box.cls || '' ) + ' ' + ( box.type || '' ),
    children: [{
        tag: 'div',
        cls: 'title',
        html: box.title
    }, {
        id: box.id + '-body',
        tag: 'div',
        cls: 'main'
    }]
}, true );
Ext.get( box.id + '-body' ).update( box.body, true );