简体   繁体   English

如何在没有eval()的情况下通过ExtJS AJAX调用执行Javascript,就像我可以使用JQuery一样?

[英]How can I execute Javascript via ExtJS AJAX call without eval() as I can with JQuery?

I would like to execute javascript in pages which I load via Ext.Ajax.request . 我想在我通过Ext.Ajax.request加载的页面中执行javascript。 To do this, I have to load the scripts and eval() them like this: 为此,我必须像这样加载脚本和eval()

Ext.Ajax.request({
    url: 'content/view_application.php',
    success: function(objServerResponse) {
        var responseText = objServerResponse.responseText;
        regionContent.update(responseText);
        var scripts, scriptsFinder=/<script[^>]*>([\s\S]+)<\/script>/gi;
        while(scripts=scriptsFinder.exec(responseText)) {
            eval(scripts[1]);
        }
    }
});

With JQuery, however, I can have Javascript executed in pages which are called via AJAX without resorting to eval() like this: 但是,使用JQuery,我可以在通过AJAX调用的页面中执行Javascript而不需要像这样使用eval()

function replaceContentOnClick(id, pageToLoad) {
    $('body').delegate(('#' + id), 'click', function(){
        $.get('content/' + pageToLoad, function(data) {
            $('#regionContent .x-panel-body').html(data);
        });
    });
}

How is it that JQuery manages to execute the javascript in the loaded page without eval() ? 如果没有eval() ,JQuery如何设法在加载的页面中执行javascript? Is there a way to load the javascript without resorting to eval() in ExtJS as well? 有没有办法加载JavaScript而不需要在ExtJS中使用eval()

Personally, I wouldn't worry about the eval statement. 就个人而言,我不担心eval声明。 I am aware that Douglas Crockford counsels against its use: 我知道Douglas Crockford建议不要使用它:

eval is evil 评估是邪恶的

The eval function (and its relatives, Function, setTimeout, and setInterval) provide access to the JavaScript compiler. eval函数(及其亲属,Function,setTimeout和setInterval)提供对JavaScript编译器的访问。 This is sometimes necessary, but in most cases it indicates the presence of extremely bad coding. 这有时是必要的,但在大多数情况下,它表明存在极其糟糕的编码。 The eval function is the most misused feature of JavaScript. eval函数是JavaScript最被误用的功能。

(from http://www.jslint.com/lint.html ) (来自http://www.jslint.com/lint.html

Here he states that "This is sometimes necessary", and I would argue that your use is a valid one. 在这里他说“这有时是必要的”,我认为你的使用是有效的。

The JSON2.js library ( http://www.json.org ) uses this command, and flags to JSLint that this is intended: JSON2.js库( http://www.json.org )使用此命令,并向JSLint标记这是预期的:

/*jslint evil: true */

Is there a particular reason that you would like to avoid its use? 是否有特殊原因要避免使用它?

Well, go to the jQuery source (http://code.jquery.com/jquery-latest.js) and Ctrl+F for globalEval: function . 好吧,转到jQuery源代码(http://code.jquery.com/jquery-latest.js)和Ctrl + F for globalEval: function This is the function which runs JavaScript. 这是运行JavaScript的功能。 You'll see it actually adds script tags into the DOM. 你会看到它实际上将脚本标签添加到DOM中。 As for extJS, I don't know. 至于extJS,我不知道。 Try searching in their source code for "script" or 'script' to see if they insert script tags anywhere in a similar way. 尝试在源代码中搜索“script”或“script”,看看他们是否以类似的方式在任何地方插入脚本标记。 Or you could just implement your own globalEval . 或者你可以实现自己的globalEval

At the risk of sounding like a broken record , please see my other answer regarding the loadScripts config. 听起来像是破纪录的风险,请参阅我关于loadScripts配置的其他答案 You should consider sticking to the same question when it's just a follow-up to what you've already asked, rather than starting brand new questions. 你应该考虑坚持同样的问题,只是对你已经问过的问题进行跟进,而不是开始提出全新的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM