无法在Sencha-Touch2中使用Ext.Ajax运行javascript?

use*_*573 3 html javascript ajax extjs sencha-touch-2

我使用的是在论坛讨论中煎茶触htmlPanel.js 这里来显示本地HTML内容.我可以使用普通的html标签加载html内容,但不能加载javascript.

下面是我使用的htmlPanel.js:

Ext.define('HTMLPanel', {
    extend: 'Ext.Panel',


// We are using Ext.Ajax, so we should require it
    requires: ['Ext.Ajax'],
    config: {
        listeners: {
            activate: 'onActivate'
        },


// Create a new configuration called `url` so we can specify the URL
        url: null        
    },


    onActivate: function(me, container) {
        Ext.Ajax.request({
// we should use the getter for our new `url` config
            url:    'htmlTest.html',//this.getUrl(),
            method: "GET",
            success: function(response, request) {
// We should use the setter for the HTML config for this
//Ext.Msg.alert('Alert', 'Success!!!', Ext.emptyFn);    
                me.setHtml(response.responseText);                
            },
            failure: function(response, request) {
//Ext.Msg.alert('Alert', 'Failure!!!', Ext.emptyFn);    
                me.setHtml("failed -- response: " + response.responseText);
            }
        });
    }
});
Run Code Online (Sandbox Code Playgroud)

下面是我的htmlTest.html:

<!DOCTYPE html>
<html>
    <body>

        <canvas id="myCanvas">Your browser does not support the HTML5 canvas tag.</canvas>

        <script type="text/javascript" charset="utf-8">
            var c=document.getElementById('myCanvas');
            var ctx=c.getContext('2d');
            ctx.fillStyle='#FF0000';
            ctx.fillRect(0,0,640,1280);
        </script>

        <h1>This is some text in a paragraph.</h1>

    </body>
</html>
Run Code Online (Sandbox Code Playgroud)

以下是我的index.js:

Ext.application({
    name: 'SampleLoad',
    launch: function () {
        //loadURL('htmlTest.html');
        Ext.Viewport.add({
                        url:    'htmlTest.html',
            xclass: "HTMLPanel",

        });

        // Add the new HTMLPanel into the viewport so it is visible
        Ext.Viewport.add(HTMLPanel);
    }
});
Run Code Online (Sandbox Code Playgroud)

我能够看到文本"有些文本在这里.",但不是我尝试使用javascript创建的画布.

是否有需要指定的配置?还是其他原因?

谢谢.

Pum*_*ers 5

问题是HTML被植入DOM中,但脚本不会被执行.这是分配给innerHtml的情况,也可能是setHtml()的情况.

您可以通过在拼接HTML后立即显式执行呈现元素中的脚本来解决此问题.您的htmlPanel.js将如下所示:

...
// We should use the setter for the HTML config for this
//Ext.Msg.alert('Alert', 'Success!!!', Ext.emptyFn);    
                me.setHtml(response.responseText);  
                var scriptArray = me.renderElement.dom.getElementsByTagName("script");   
                for(var i=0;i<scriptArray.length;i++) {  
                   eval(scriptArray[i].text);  
                }              
            },
...
Run Code Online (Sandbox Code Playgroud)