不经确认直接关闭IE浏览器所有实例的方法 | #javascript #ie

写脚本时不小心发现了一个新的不经过确认直接关闭IE窗口的方法不过还是ie-only.而且,此方法有个非常恐怖的地方,它不是单纯关闭窗口,而是关闭整个浏览器,ie7(无论多少实例),还有maxthon等都不能幸免于难.IE8会自动recover页面。

在我机子上是这样的, 别的机子就不得而知了,麻烦大家给看看, 是否通用.
偶然发现,但不明其理,麻烦高手解释下。

<script>
document.onclick = function() {
closeWin(event.srcElement);
}
function closeWin(o)
{
var aElm = document.createElement('a');
aElm.href = o.href;
var imgEml = document.createElement('img');
aElm.appendChild(imgEml);
document.body.appendChild(aElm);
aElm.click();
}
</script>
<input type="button" onclick="closeWin(this);return false;" value="关闭窗口">

帮你分析下函数
document.onclick = function() {

closeWin(event.srcElement);

}
这个函数很奇怪 给浏览器document加上了onClick事件 也就是说点击浏览器任何位置都会触发closeWin这个函数,并且将事件点击位置的元素对象传过去

function closeWin(o)

{

var aElm = document.createElement('a');

//这里创建了a标签
aElm.href = o.href;

//将a标签的href属性设置成事件源的href
//实际上你现在的事件源不论是button 或者是document都没这个属性
//所以会将undifine传过来 不过没关系 继续
var imgEml = document.createElement('img');

//创建img 对象
aElm.appendChild(imgEml);

//将a 标签加入到img 对象中 有这种写法么? 我也不知道 暂且不管 继续

document.body.appendChild(aElm);
//这句起始就是显示 img 对象

aElm.click();
//让它实现onclick时间
//至此没人任何关闭window的方法

}
我也帮你调试了下代码IE6下如果点button 将引起一个内存错误 导致浏览器崩溃推出
如果点击其他地方 则引起死循环 应该是aElm.click();引起的 它又触发document.click事件 触发winClose函数 最终栈溢出 浏览器还是崩溃
所以你这个函数完全是个错误函数 别用了