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

时间:2022-05-14 20:38:47

I would like to execute javascript in pages which I load via Ext.Ajax.request. To do this, I have to load the scripts and eval() them like this:

我想在我通过Ext.Ajax.request加载的页面中执行javascript。为此,我必须像这样加载脚本和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()? Is there a way to load the javascript without resorting to eval() in ExtJS as well?

如果没有eval(),JQuery如何设法在加载的页面中执行javascript?有没有办法加载JavaScript而不需要在ExtJS中使用eval()?

3 个解决方案

#1


3  

Personally, I wouldn't worry about the eval statement. I am aware that Douglas Crockford counsels against its use:

就个人而言,我不担心eval声明。我知道Douglas Crockford建议不要使用它:

eval is evil

eval是邪恶的

The eval function (and its relatives, Function, setTimeout, and setInterval) provide access to the JavaScript compiler. 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函数(及其relatives,Function,setTimeout和setInterval)提供对JavaScript编译器的访问。这有时是必要的,但在大多数情况下,它表明存在极其糟糕的编码。 eval函数是JavaScript最被误用的功能。

(from 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?

是否有特殊原因要避免使用它?

#2


1  

Well, go to the jQuery source (http://code.jquery.com/jquery-latest.js) and Ctrl+F for globalEval: function. This is the function which runs JavaScript. You'll see it actually adds script tags into the DOM. As for extJS, I don't know. Try searching in their source code for "script" or 'script' to see if they insert script tags anywhere in a similar way. Or you could just implement your own globalEval.

好吧,转到jQuery源代码(http://code.jquery.com/jquery-latest.js)和Ctrl + F for globalEval:function。这是运行JavaScript的功能。你会看到它实际上将脚本标签添加到DOM中。至于extJS,我不知道。尝试在源代码中搜索“script”或“script”,看看他们是否以类似的方式在任何地方插入脚本标记。或者你可以实现自己的globalEval。

#3


1  

At the risk of sounding like a broken record, please see my other answer regarding the loadScripts config. 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.

听起来像是破纪录的风险,请参阅我关于loadScripts配置的其他答案。你应该考虑坚持同样的问题,这只是对你已经问过的问题的跟进,而不是开始提出全新的问题。

#1


3  

Personally, I wouldn't worry about the eval statement. I am aware that Douglas Crockford counsels against its use:

就个人而言,我不担心eval声明。我知道Douglas Crockford建议不要使用它:

eval is evil

eval是邪恶的

The eval function (and its relatives, Function, setTimeout, and setInterval) provide access to the JavaScript compiler. 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函数(及其relatives,Function,setTimeout和setInterval)提供对JavaScript编译器的访问。这有时是必要的,但在大多数情况下,它表明存在极其糟糕的编码。 eval函数是JavaScript最被误用的功能。

(from 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?

是否有特殊原因要避免使用它?

#2


1  

Well, go to the jQuery source (http://code.jquery.com/jquery-latest.js) and Ctrl+F for globalEval: function. This is the function which runs JavaScript. You'll see it actually adds script tags into the DOM. As for extJS, I don't know. Try searching in their source code for "script" or 'script' to see if they insert script tags anywhere in a similar way. Or you could just implement your own globalEval.

好吧,转到jQuery源代码(http://code.jquery.com/jquery-latest.js)和Ctrl + F for globalEval:function。这是运行JavaScript的功能。你会看到它实际上将脚本标签添加到DOM中。至于extJS,我不知道。尝试在源代码中搜索“script”或“script”,看看他们是否以类似的方式在任何地方插入脚本标记。或者你可以实现自己的globalEval。

#3


1  

At the risk of sounding like a broken record, please see my other answer regarding the loadScripts config. 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.

听起来像是破纪录的风险,请参阅我关于loadScripts配置的其他答案。你应该考虑坚持同样的问题,这只是对你已经问过的问题的跟进,而不是开始提出全新的问题。