为什么在铬,Safari浏览器的JavaScript的eval的工作,但在Firefox上,只有当萤火虫? [英] Why would javascript eval work in chrome, safari, but in firefox only when firebug on?
问题描述
感谢<一href="http://stackoverflow.com/questions/4333326/why-doesnt-javascript-execute-in-php-file-loaded-with-ext-ajax-request/4333374#4333374">this回答我能得到JavaScript的时候通过ExtJS的AJAX调用加载执行。
但是,下面的code没有在所有浏览器相同的(在Mac上):
- 镀铬:作品,警报弹出
- 在野生动物园:作品,警报弹出
- 在Firefox中:作品的但只有当萤火虫启用的,当萤火虫未启用,该脚本将被忽略
如何获得的JavaScript在Firefox中通过AJAX调用来执行,而无需安装萤火虫?
Ext.onReady(函数(){
VAR menuItemStart =新Ext.Panel({
ID:panelStart,
标题:开始,
HTML:这是启动菜单项。,
CLS:'菜单项
});
VAR menuItemApplication =新Ext.Panel({
ID:panelApplication,
标题:应用程序,
HTML:这是申请页面,
CLS:'菜单项
});
VAR regionMenu =新Ext.Panel({
区域:西方,
驳:真,
宽度:210,
布局:手风琴,
layoutConfig:{
动画:真
},
项目:[menuItemStart,menuItemApplication]
});
VAR regionContent =新Ext.Panel({
编号:'含量 - 面积,
区域:中心,
填充:'10',
自动滚屏:真正的,
HTML:'这是内容的
});
新Ext.Viewport({
布局:边境,
项目:[regionMenu,regionContent]
});
menuItemStart.header.on('点击',函数(){
Ext.Ajax.request({
网址:内容/ view_start.php',
成功:函数(objServerResponse){
regionContent.update(objServerResponse.responseText);
}
});
});
menuItemApplication.header.on('点击',函数(){
Ext.Ajax.request({
网址:内容/ view_application.php',
成功:函数(objServerResponse){
VAR的responseText = objServerResponse.responseText;
执行console.log(responseText的);
regionContent.update(responseText的);
VAR脚本,scriptsFinder = /&LT;脚本[^&GT;] *&GT;([\ S \ S] +)&LT; \ / SCRIPT&GT; / GI;
而(脚本= scriptsFinder.exec(responseText的)){
的eval(脚本[1]);
}
}
});
});
});
这是正装的基础:
&LT;脚本类型=文/ JavaScript的&GT;
警报(内部应用程序视图');
&LT; / SCRIPT&GT;
&LT; PHP
回声这是在应用程序视图。日期(Y-M-D H:我:S');
?&GT;
的console.log
是罪魁祸首。
每当你登录的JavaScript,这是一个好主意,以检查是否控制台调用它的函数之前存在的:
如果(window.console)的console.log('不管');
这样的话,它只会在控制台可以执行。
Thanks to this answer I was able to get javascript to execute when being loaded via an ExtJS AJAX call.
However, the following code does not work the same in all browsers (on mac):
- chrome: works, alert pops up
- safari: works, alert pops up
- firefox: works, but only when Firebug is enabled, when Firebug is not enabled, the script is ignored
How can I get javascript to execute via AJAX call in Firefox without having Firebug installed?
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) {
var responseText = objServerResponse.responseText;
console.log(responseText);
regionContent.update(responseText);
var scripts, scriptsFinder=/<script[^>]*>([\s\S]+)<\/script>/gi;
while(scripts=scriptsFinder.exec(responseText)) {
eval(scripts[1]);
}
}
});
});
});
The base that is being loaded:
<script type="text/javascript">
alert('inside application view');
</script>
<?php
echo 'this is the application view at ' . date('Y-m-d h:i:s');
?>
console.log
is your culprit.
Whenever you're logging in JavaScript, it's a good idea to check if console exists before calling it's functions:
if(window.console) console.log('whatever');
That way, it will only execute when the console is available.
这篇关于为什么在铬,Safari浏览器的JavaScript的eval的工作,但在Firefox上,只有当萤火虫?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!