为什么 javascript 在加载了 Ext.Ajax.Request 的 .php 文件中不执行? [英] Why doesn't javascript execute in .php file loaded with Ext.Ajax.Request?

查看:22
本文介绍了为什么 javascript 在加载了 Ext.Ajax.Request 的 .php 文件中不执行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过 ajax 加载 .php 文件,它在加载时执行 ExtJS 脚本,进而修改 DOM 中已经存在的现有 ExtJS 对象.

I want to load .php files via ajax which execute ExtJS script as they load, which in turn modifies the existing ExtJS objects already present in the DOM.

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

However, I can't even get Javascript to execute from a page that is being loaded via Ext.Ajax.request. And no errors are showing up in the Firebug Net panel. The PHP code gets executed, but not the Javascript. When I call the page being loaded by itself in the browser, it executes the Javascript fine.

如何让 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 加载的文件:

the file that is being loaded via 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');
?>

推荐答案

当你得到 ajax 响应时,窗口上的 onload 事件已经被触发,所以函数不会被执行,因为 onload 事件不会被触发再次.仅尝试使用警报:

When you get the ajax response the onload event on the window has been already fired so the function won't be executed because the onload event won't be fired again. Try only with the alert:

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

更新

浏览器不会以这种方式执行注入的脚本,因此您可以尝试使用以下内容:

Browsers don't execute injected scripts in that way so you can try with something like:

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

这篇关于为什么 javascript 在加载了 Ext.Ajax.Request 的 .php 文件中不执行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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