3

ここのフォーラムで議論されたSencha-Touchの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);
            }
        });
    }
});

以下は私の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>

そして以下は私の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);
    }
});

「一部のテキストはここにあります。」というテキストは表示されますが、javascriptを使用して作成しようとしたキャンバスは表示されません。

指定する必要のある構成はありますか?または他の原因はありますか?

ありがとう。

4

1 に答える 1

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);  
                }              
            },
...
于 2012-09-23T12:14:13.907 に答える