2010-12-02 49 views
0

我想通过ajax加载.php文件,它们在加载时执行ExtJS脚本,这又会修改DOM中已有的ExtJS对象。为什么JavaScript不会在加载Ext.Ajax.Request的.php文件中执行?

但是,我甚至无法从通过Ext.Ajax.request加载的页面执行Javascript。并且Firebug Net面板中没有显示错误。 PHP代码被执行,但不是Javascript。当我在浏览器中调用自己加载的页面时,它会执行Javascript罚款。

我怎样才能获得Javascript在执行页面加载Ext.Ajax.request?

Ext.onReady(function(){ 

    var menuItemStart = new Ext.Panel({ 
     id: 'panelStart', 
     title: 'Start', 
     html: 'This is the start menu item.', 
     cls:'menuItem' 
    }); 

    var menuItemApplication = new Ext.Panel({ 
     id: 'panelApplication', 
     title: 'Application', 
     html: 'this is the application page', 
     cls:'menuItem' 
    }); 

    var regionMenu = new Ext.Panel({ 
     region:'west', 
     split:true, 
     width: 210, 
     layout:'accordion', 
     layoutConfig:{ 
      animate:true 
     }, 
     items: [ menuItemStart, menuItemApplication ] 
    }); 

    var regionContent = new Ext.Panel({ 
     id: 'contentArea', 
     region: 'center', 
     padding:'10', 
     autoScroll: true, 
     html: 'this is the content' 
    }); 

    new Ext.Viewport({ 
     layout: 'border', 
     items: [ regionMenu, regionContent ] 
    }); 

    menuItemStart.header.on('click', function() { 
     Ext.Ajax.request({ 
      url: 'content/view_start.php', 
      success: function(objServerResponse) { 
       regionContent.update(objServerResponse.responseText); 
      } 
     }); 
    }); 

    menuItemApplication.header.on('click', function() { 
     Ext.Ajax.request({ 
      url: 'content/view_application.php', 
      success: function(objServerResponse) { 

       regionContent.update(objServerResponse.responseText); 
      } 
     }); 
    }); 
}); 

正在被通过Ajax加载的文件:

<script type="text/javascript"> 
    window.onload=function() { 
     alert('from application view'); //is not executed 
    } 

    //Ext.onReady(function(){ 
    // alert('from application view extjs'); //is not executed 
    //} 
</script> 
<?php 
echo 'this is the application view at ' . date('Y-m-d h:i:s'); 
?> 

回答

2

当你的Ajax响应窗口上的onload事件已经被解雇,因此功能会不会因为执行onload事件不会再次被触发。仅与警报尝试:

<script type="text/javascript"> 
    alert('from application view'); 
</script> 
<?php 
echo 'this is the application view at ' . date('Y-m-d h:i:s'); 
?> 

UPDATE

浏览器不以这种方式执行注入脚本,所以你可以用类似尝试:

var scripts, scriptsFinder=/<script[^>]*>([\s\S]+)<\/script>/gi; 
while(scripts=scriptsFinder.exec(responseText)) 
{ 
    eval(scripts[1]); 
} 
+0

这就是我原来的样子,但它也不执行,在Firebug Net面板中没有错误,就好像javascript被忽略了,我是否需要使用responseText以外的东西?但responseBody甚至不会出现PHP生成的内容 – 2010-12-02 09:25:18

2

你试过路过true第二个参数为Panel.load(恰好是loadScripts选项)?

regionContent.update(objServerResponse.responseText, true); 
0

通常情况下,当你调用更新转你只是做 更新(字符串,真实),它会执行包含在字符串中的脚本。但是,ext核心似乎缺少这种功能,但没有关于更新方法的文档(我不得不搜索实际的代码来确认这一点)。

如果您使用某些常规EXT(如ext-all)可以简单地添加

regionContent.update(objServerResponse.responseText,true); 

像这样,它应该评估脚本。尽管我没有骰子,但是 - ext-all速度太慢,但我需要eval功能。我可能不得不砍掉EXT。

相关问题