<!--1.htm-->
<script type="text/javascript">
<!--
var _alert = alert;
window.alert = function(s)
{
_alert("aaa");
_alert(s);
}
function ttt(ss)
{
alert(ss);
}
//-->
</script>
<html>
<body>
<input type="button" onclick=ttt("Hello") value="test" />
</body>
</html>
<script type="text/javascript">
<!--
var _alert = alert;
window.alert = function(s)
{
_alert("aaa");
_alert(s);
}
function ttt(ss)
{
alert(ss);
}
//-->
</script>
<html>
<body>
<input type="button" onclick=ttt("Hello") value="test" />
</body>
</html>
<!--
var _alert = alert;
window.alert = function(s)
{
_alert("aaa");
_alert(s);
}
alert(alert==window.alert);//可以看出alert与window.alert并不是同一个对象
window.alert(alert==window.alert);
function ttt(ss)
{
alert(ss);
}
function ttt(ss)
{
window.alert(ss);
}
//-->
</script>
<html>
<body>
<input type="button" onclick=ttt("Hello") value="test" />
<input type="button" onclick=ttt1("Hello") value="test1" />
</body>
</html>
<!--
var _alert = alert;
window.alert = function(s)
{
_alert("aaa");
_alert(s);
} alert(alert==window.alert);//可以看出alert与window.alert并不是同一个对象
window.alert(alert==window.alert);function ttt(ss)
{
alert(ss);
}
function ttt1(ss)
{
window.alert(ss);
}
//-->
</script>
<html>
<body>
<input type="button" onclick=ttt("Hello") value="test" />
<input type="button" onclick=ttt1("Hello") value="test1" />
</body>
</html>
<!--
var alert = function(s)
{
document.write(s);
};function ttt(ss)
{
alert(ss);
}
//-->
</script>
<html>
<body>
<input type="button" onclick=ttt("Hello") value="test" />
</body>
</html>
至于为什么会出错,不清楚,有高手的话来解释一下吧。
<script type="text/javascript">
var _alert = alert;
</script>
<script type="text/javascript">
var alert = function(s)
{
_alert("aaa");
_alert(s);
};
function ttt(ss)
{
alert(ss);
}
</script>
<html>
<body>
<input type="button" onclick=ttt("Hello") value="test" />
</body>
</html>
http://hax.javaeye.com/blog/349569
为啥要改alert?这种内置函数还是少动为妙。
应该 IE 的脚本引擎对 Script 标记顺序编译滴结果,改进了一下 #7 滴代码,更符合 lz 滴想法,
L@_@K
<script type="text/javascript">
var _alert = window.alert;
</script>
<script type="text/javascript">
window.alert = function(s)
{
_alert("override alert!");
_alert(s);
};
</script>
</head> <body>
<input type="button" onclick="alert('csdn!')" value="test" />
</body>
顺序执行时,第一段 script 中的 _alert 引用了全局 window.alert,接下来滴 script 中 window.alert 被改写了,其中继续调用了 _alert!
L@_@K
<script type="text/javascript">
<!--
var _alert = window.alert;// 打开这句就玩完了?!!
// 包括后三句滴输出结果也不一样了!
//document.write("_alert==alert ", _alert==alert,"<br /><br />");window.alert = function(s)
{
_alert("aaa");
_alert(s);
};
document.write("window.alert==alert ", window.alert==alert, "<br />");
document.write("_alert==window.alert ", _alert==window.alert, "<br />");
document.write("_alert==alert ", _alert==alert,"<br />");// 打开前//window.alert==alert true
//_alert==window.alert false
//_alert==alert false// 打开后//_alert==alert true
//
//window.alert==alert false
//_alert==window.alert false
//_alert==alert truefunction ttt(ss)
{
alert(ss);
}
//-->
</script> </head> <body>
<input type="button" onclick=ttt("Hello") value="test" />
</body>
var _alert = window.alert;
就可以了