判断目标是否是window,document,和拥有tagName的Element的代码_Javascript教程-查字典教程网
判断目标是否是window,document,和拥有tagName的Element的代码
判断目标是否是window,document,和拥有tagName的Element的代码
发布时间:2016-12-30 来源:查字典编辑
摘要:复制代码代码如下:functionisWindow(obj){if(typeofobj.closed=='undefined')return...

复制代码 代码如下:

function isWindow( obj )

{

if( typeof obj.closed == 'undefined' ) return false;

var result = /[object (window|global)]/i.test( Object.prototype.toString.call( obj ) );

if( result )return result;

try{

obj.closed = obj.closed;

return false;

}catch(e)

{

result = true;

}

return result;

}

function isDocument( obj )

{

if( typeof obj.body == 'undefined' ) return false;

var b = obj.body;

try{

obj.body = null;

obj.body = b;

return false;

}catch(e)

{

return true;

}

}

function isElement( o )

{

var tn = 'tagName',temp = o[tn],result;

if( typeof temp == 'undefined' )return false;

try{

o[tn] = null;

result = ( temp == o[tn] );

o[tn] = temp;

return result;

}catch(e)

{

return true;

}

}

function getOwnerWindow( node )

{

if( isWindow( node ) )return node;

var doc = isDocument( node ) ? node : node.ownerDocument;

return doc.view || doc.parentWindiw || doc.defaultView;

}

需要充分测试

相关阅读
推荐文章
猜你喜欢
附近的人在看
推荐阅读
拓展阅读
  • 大家都在看
  • 小编推荐
  • 猜你喜欢
  • 最新Javascript教程学习
    热门Javascript教程学习
    编程开发子分类