admin管理员组文章数量:1332865
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({
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:
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?
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({
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:
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?
-
2
I can answer only your first question: jquery looks at the Content-Type http header and if it is
application/json
it will do the necessary conversion before invoking thesuccess
callback. – Darin Dimitrov Commented Dec 2, 2010 at 10:54
3 Answers
Reset to default 3Personally, I wouldn't worry about the eval
statement. I am aware that Douglas Crockford counsels against its use:
eval is evil
The eval function (and its relatives, Function, setTimeout, and setInterval) provide access to the JavaScript piler. 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.
(from http://www.jslint./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) uses this mand, and flags to JSLint that this is intended:
/*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./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
.
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.
本文标签: How can I execute Javascript via ExtJS AJAX call without eval() as I can with JQueryStack Overflow
版权声明:本文标题:How can I execute Javascript via ExtJS AJAX call without eval() as I can with JQuery? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742293224a2448205.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论