javascript在不同浏览器中的类型测试实验(跨浏览器编程要注意了!!)
2010-08-10 13:11
309 查看
为了测试javascript中typeof和Object.prototype.toString的各个native object在不同的浏览器的运行结果,特写了以下的测试代码:
var def;
var gPrimitiveType = {
'string':'str',
'boolean':true,
'function':function(){},
'object':{},
'array':[],
'undefined':def
};
var ul = $('content');
QQVIP.object.each(gPrimitiveType, function(value,key){
var child = document.createElement('LI');
var text = 'typeof ' + key + ' is:' + (typeof value);
child.appendChild(document.createTextNode(text));
ul.appendChild(child);
});
var gProtoType = {
'String.prototype':String.prototype,
'Function.prototype':Function.prototype,
'Object.prototype':Object.prototype,
'Array.prototype':Array.prototype,
'Date.prototype':Date.prototype,
'RegExp.prototype':RegExp.prototype
};
var ul = $('typelist');
QQVIP.object.each(gProtoType, function(value,key){
var child = document.createElement('LI');
var text = 'Object.prototype.toString.call( ' + key + ') is:' + (Object.prototype.toString.call(value) );
child.appendChild(document.createTextNode(text));
ul.appendChild(child);
});
下面是在各个浏览器下的运行结果:
1,IE6下:
typeof 运算结果:
typeof string is:string
typeof boolean is:boolean
typeof function is:function
typeof object is:object
typeof array is:object
typeof undefined is:undefined
Object.prototype.toString.call() 运算结果:
Object.prototype.toString.call( String.prototype) is:[object Object]
Object.prototype.toString.call( Function.prototype) is:[object Function]
Object.prototype.toString.call( Object.prototype) is:[object Object]
Object.prototype.toString.call( Array.prototype) is:[object Array]
Object.prototype.toString.call( Date.prototype) is:[object Date]
Object.prototype.toString.call( RegExp.prototype) is:[object RegExp]
二,Firefox3.0.15
typeof 运算结果:
typeof string is:string
typeof boolean is:boolean
typeof function is:function
typeof object is:object
typeof array is:object
typeof undefined is:undefined
Object.prototype.toString.call() 运算结果:
Object.prototype.toString.call( String.prototype) is:[object String]
Object.prototype.toString.call( Function.prototype) is:[object Function]
Object.prototype.toString.call( Object.prototype) is:[object Object]
Object.prototype.toString.call( Array.prototype) is:[object Array]
Object.prototype.toString.call( Date.prototype) is:[object Date]
Object.prototype.toString.call( RegExp.prototype) is:[object RegExp]
三,Chrome4.0
typeof 运算结果:
typeof string is:string
typeof boolean is:boolean
typeof function is:function
typeof object is:object
typeof array is:object
typeof undefined is:undefined
Object.prototype.toString.call() 运算结果:
Object.prototype.toString.call( String.prototype) is:[object String]
Object.prototype.toString.call( Function.prototype) is:[object Function]
Object.prototype.toString.call( Object.prototype) is:[object Object]
Object.prototype.toString.call( Array.prototype) is:[object Array]
Object.prototype.toString.call( Date.prototype) is:[object Date]
Object.prototype.toString.call( RegExp.prototype) is:[object Object]
四:Opera9.20
typeof 运算结果:
typeof string is:string
typeof boolean is:boolean
typeof function is:function
typeof object is:object
typeof array is:object
typeof undefined is:undefined
Object.prototype.toString.call() 运算结果:
Object.prototype.toString.call( String.prototype) is:[object String]
Object.prototype.toString.call( Function.prototype) is:[object Function]
Object.prototype.toString.call( Object.prototype) is:[object Object]
Object.prototype.toString.call( Array.prototype) is:[object Array]
Object.prototype.toString.call( Date.prototype) is:[object Date]
Object.prototype.toString.call( RegExp.prototype) is:[object RegExp]
五,Safari3.1.2
typeof 运算结果:
typeof string is:string
typeof boolean is:boolean
typeof function is:function
typeof object is:object
typeof array is:object
typeof undefined is:undefined
Object.prototype.toString.call() 运算结果:
Object.prototype.toString.call( String.prototype) is:[object String]
Object.prototype.toString.call( Function.prototype) is:[object Function]
Object.prototype.toString.call( Object.prototype) is:[object Object]
Object.prototype.toString.call( Array.prototype) is:[object Array]
Object.prototype.toString.call( Date.prototype) is:[object Date]
Object.prototype.toString.call( RegExp.prototype) is:[object RegExpPrototype]
综上,标红部分都是跟其他不一样的,需要充分注意。
var def;
var gPrimitiveType = {
'string':'str',
'boolean':true,
'function':function(){},
'object':{},
'array':[],
'undefined':def
};
var ul = $('content');
QQVIP.object.each(gPrimitiveType, function(value,key){
var child = document.createElement('LI');
var text = 'typeof ' + key + ' is:' + (typeof value);
child.appendChild(document.createTextNode(text));
ul.appendChild(child);
});
var gProtoType = {
'String.prototype':String.prototype,
'Function.prototype':Function.prototype,
'Object.prototype':Object.prototype,
'Array.prototype':Array.prototype,
'Date.prototype':Date.prototype,
'RegExp.prototype':RegExp.prototype
};
var ul = $('typelist');
QQVIP.object.each(gProtoType, function(value,key){
var child = document.createElement('LI');
var text = 'Object.prototype.toString.call( ' + key + ') is:' + (Object.prototype.toString.call(value) );
child.appendChild(document.createTextNode(text));
ul.appendChild(child);
});
下面是在各个浏览器下的运行结果:
1,IE6下:
typeof 运算结果:
typeof string is:string
typeof boolean is:boolean
typeof function is:function
typeof object is:object
typeof array is:object
typeof undefined is:undefined
Object.prototype.toString.call() 运算结果:
Object.prototype.toString.call( String.prototype) is:[object Object]
Object.prototype.toString.call( Function.prototype) is:[object Function]
Object.prototype.toString.call( Object.prototype) is:[object Object]
Object.prototype.toString.call( Array.prototype) is:[object Array]
Object.prototype.toString.call( Date.prototype) is:[object Date]
Object.prototype.toString.call( RegExp.prototype) is:[object RegExp]
二,Firefox3.0.15
typeof 运算结果:
typeof string is:string
typeof boolean is:boolean
typeof function is:function
typeof object is:object
typeof array is:object
typeof undefined is:undefined
Object.prototype.toString.call() 运算结果:
Object.prototype.toString.call( String.prototype) is:[object String]
Object.prototype.toString.call( Function.prototype) is:[object Function]
Object.prototype.toString.call( Object.prototype) is:[object Object]
Object.prototype.toString.call( Array.prototype) is:[object Array]
Object.prototype.toString.call( Date.prototype) is:[object Date]
Object.prototype.toString.call( RegExp.prototype) is:[object RegExp]
三,Chrome4.0
typeof 运算结果:
typeof string is:string
typeof boolean is:boolean
typeof function is:function
typeof object is:object
typeof array is:object
typeof undefined is:undefined
Object.prototype.toString.call() 运算结果:
Object.prototype.toString.call( String.prototype) is:[object String]
Object.prototype.toString.call( Function.prototype) is:[object Function]
Object.prototype.toString.call( Object.prototype) is:[object Object]
Object.prototype.toString.call( Array.prototype) is:[object Array]
Object.prototype.toString.call( Date.prototype) is:[object Date]
Object.prototype.toString.call( RegExp.prototype) is:[object Object]
四:Opera9.20
typeof 运算结果:
typeof string is:string
typeof boolean is:boolean
typeof function is:function
typeof object is:object
typeof array is:object
typeof undefined is:undefined
Object.prototype.toString.call() 运算结果:
Object.prototype.toString.call( String.prototype) is:[object String]
Object.prototype.toString.call( Function.prototype) is:[object Function]
Object.prototype.toString.call( Object.prototype) is:[object Object]
Object.prototype.toString.call( Array.prototype) is:[object Array]
Object.prototype.toString.call( Date.prototype) is:[object Date]
Object.prototype.toString.call( RegExp.prototype) is:[object RegExp]
五,Safari3.1.2
typeof 运算结果:
typeof string is:string
typeof boolean is:boolean
typeof function is:function
typeof object is:object
typeof array is:object
typeof undefined is:undefined
Object.prototype.toString.call() 运算结果:
Object.prototype.toString.call( String.prototype) is:[object String]
Object.prototype.toString.call( Function.prototype) is:[object Function]
Object.prototype.toString.call( Object.prototype) is:[object Object]
Object.prototype.toString.call( Array.prototype) is:[object Array]
Object.prototype.toString.call( Date.prototype) is:[object Date]
Object.prototype.toString.call( RegExp.prototype) is:[object RegExpPrototype]
综上,标红部分都是跟其他不一样的,需要充分注意。
相关文章推荐
- 不同浏览器基于javascript测试结果
- javascript根据不同类型的浏览器,获取其绝对路径,谷歌除外.
- 不同浏览器基于javascript测试结果
- javascript 操作日期 测试(注意浏览器兼容问题)
- javascript 操作日期 测试(注意浏览器兼容问题)
- 用javascript判断不同的浏览器类型以调用不用的css
- 关于javascript判断不同浏览器的类型和版本号
- 不同浏览器基于javascript测试结果
- 不同浏览器基于javascript测试结果
- 不同浏览器基于javascript测试结果
- JavaScript为不同浏览器添加事件
- 网页在不同的浏览器下的兼容性的测试工具
- Javascript兼容性问题小结(容易导致浏览器不同,又不容易察觉的)
- Javascript 判断客户端浏览器类型代码
- 浏览器Javascript性能测试网址
- css3-自适应页面的实现(响应式设计、响应不同类型设备)、浏览器适配
- jQuery javaScript js 判断浏览器的类型、版本的方法
- JavaScript中的document.referrer在各种浏览器测试结果
- JavaScript不同数据类型的转换
- javascript 学习笔记(六)浏览器类型及版本信息检测代码