Showing posts with label null. Show all posts
Showing posts with label null. Show all posts

Friday, January 28, 2011

IE throw null object exception when flash call javascript function

用open flash chart第一次正常载入一个图表到页面之后,当使用jQuery.fn.empty()方法移除此图表时,IE中会抛出一个错误,其他浏览器都是正常的,内容如下:


JScript - script block, line 1 character 124
'null' is null or not an object

通过IE8的debug工具可以看到出错时,javascript正在执行的代码如下:

try { document.getElementById("report-charts").SetReturnValue(__flash__toXML(ofc_resize([66,-96,66,-87])) ); } catch (e) { document.getElementById("report-charts").SetReturnValue("<undefined/>"); }

这个并不是页面中的javascript代码,应该是flash中调用外部javascript方法时,所使用的javascript代码,在IE8中断点调试,可以发现document.getElementById("report-charts")的结果为null,所以抛出了以上错误。
避过此问题的办法是在陊除已经载入的图表时,不调用jQuery.fn.empty()方法,而是直接使用jQuery.fn.html(''),将flash所在父元素的内容置空:

$('#report-charts').parent().html('');
// 如果使用$('#report-charts').parent().empty();在IE中则会报错
// ... 重新生成report-charts对象并载入新的flash图表
swfobject.embedSWF(ofc, "report-charts", "100%", "300", "9.0.0", "expressInstall.swf", {"get-data":"get_chart_0"});

Sunday, December 19, 2010

javascript中array方法调用返回window对象

array中的很多方法通过call和apply调用时会返回window对象,如下写法在Firefox、Chrome等浏览器中会取到window对象:


window === ([]).sort.call();
window === ([]).reverse.call();
([]).concat.call()[0] === window


可以将这些array的方法重写,避免它在运行时的this指向window,如重写sort方法:

Array.prototype.sort = (function(sort) { return function(callback) {
return (this == window) ? null : (callback ? sort.call(this, function(a,b) {
return callback(a,b)}) : sort.call(this));
}})(Array.prototype.sort);

Sunday, April 19, 2009

javascript compare operations

'' == '0'; // false
0 == ''; // true
0 == '0'; // true
false == 'false'; // false
false == '0'; // true
false == undefined; // false
false == null; // false
null == undefined; // true
' \t\r\n ' == 0; // true