如何在runtime重载javascript alert函数

[code="html"]

function cmd() { //precmd = "window.alert = function(str){ alert("aa" + str)};"; cmd1 = "window.alert = function(str){ alert(\"aa\" + str)} "; eval(cmd1); eval(alert("hello")); }



[/code]

想在运行时重载alert,没有成功。

eval(precmd + ";" + alert("bb"))刚试了会先执行alert("bb")
这样可以的
[code="js"]
function cmd()

{

var precmd = "var alt=window.alert;window.alert=function(str){alt('aa' + str)}; alert('hello')";
eval(precmd);
alert("bb");
}
[/code]

[code="html"]
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

window.onload=function(){ cmd=function(){ var alt=window.alert; return function (){ window.alert=function(str){alt("aa" + str)}; alert('hello'); }}() }





[/code]

你的想法有点问题,既然要重载它,那怎么还能在方法里又调用这个方法
这样是可以的:
[code="js"]
var alert = function(str){return("aa"+str)};
var a = alert('hello');
document.write(a);
[/code]

kjah 没想到这样写呢 学习了~~

你说的动态是指啥样的,这也可以拼成字符串再eval的啊