相关文章
更多最近更新
更多如何在runtime重载javascript alert函数
2019-03-25 13:45|来源: 网路
<html> <head> <script type="text/javascript"> function cmd() { //precmd = "window.alert = function(str){ alert("aa" + str)};"; cmd1 = "window.alert = function(str){ alert(\"aa\" + str)} "; eval(cmd1); eval(alert("hello")); } </script> </head> <body> <input type="button" onclick="cmd()" value="eval" /> </body> </html>
想在运行时重载alert,没有成功。
问题补充:
kjah 写道
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <script type="text/javascript"> window.onload=function(){ cmd=function(){ var alt=window.alert; return function (){ window.alert=function(str){alt("aa" + str)}; alert('hello'); }}() } </script> </head> <body> <input type="button" onclick="cmd()" value="rewrite" /> </body> </html>
这种实现要预先定义好一个script,不符合runtime动态改变的要求。
问题补充:
<html> <body> <script type="text/javascript"> function cmd() { var precmd = document.getElementById("aa").value; alert(precmd); eval(precmd + ";" + alert("bb")) } </script> <form > Script: <input id="aa" type="text" name="script" value = "window.alert = function(str) { alert('aa' + str)}" /><br /> <input type="submit" value="Submit" onclick="cmd()" /> </form> </body>
开始没描述清楚,对不起各位了,问题的本意是在一个input里重载alert,在进入cmd函数以后,理论上eval里面的alert会被覆盖,这段代码在firefox extention 里面可以过工作,但是在html中失败。
问题补充:
lixiaokui11 写道
你说的动态是指啥样的,这也可以拼成字符串再eval的啊
runtime 指的就是eval(str),比如evla(alert("aa")),把重载过的alert放在alert("aa")前。
问题补充:
lixiaokui11 写道
你说的动态是指啥样的,这也可以拼成字符串再eval的啊
runtime 指的就是eval(str),比如evla(alert("aa")),把重载过的alert放在alert("aa")前。
相关问答
更多-
-
UWP应用程序上下文不支持警报功能。 你必须使用 var dialog = new Windows.UI.Popups.MessageDialog("Hello"); dialog.showAsync(); https://msdn.microsoft.com/library/windows/apps/br208674?cs-save-lang=1&cs-lang=javascript#code-snippet-1 The alert function is not supported in UWP ap ...
-
删除delete_fn('111-aaaa')周围的单引号,如下所示 function test_fn1() { document.getElementById("demo").innerHTML = "delete"; }; Remove single quotes from onclick around delete_fn('111-aaaa') like following function test_fn ...
-
尝试这个: echo ""; Try this: echo "";
-
尝试 - alert("S'il vous plaît entrer une adresse email valide!"); 这可能是最简单的方法。 无论何时您需要在字符串中传递单引号,将其包含在双引号内,反之亦然。 如果你在一个字符串中有单引号和双引号的混合,那么把它用单引号或双引号括起来,然后使用单个反斜线将字符串中相应的字符串转义出来 - alert("So she said, \"Hey!, how are you?\". I said, 'I am fine, thanks'.") Try ...
-
如何在$ .post()回调之前调用函数显示alert()?(How to call a function before $.post() callback displays alert()?)[2023-09-13]
一个简单的方法是使用ajaxComplete,ajaxSuccess等http://api.jquery.com/category/ajax/ 这些是全局的ajax处理程序,它们可以获取通过jQuery发送的所有ajax请求...所以你可以在那里设置一个处理程序并将你自己的回调元素列入白名单等,这样你就可以忽略那些不关心的问题。 在我的测试中,这些全局处理程序在特定ajax调用的处理程序之前触发。 One easy way is with ajaxComplete, ajaxSuccess etc http ... -
只需在for循环中建立一个字符串然后提醒它 var myString; forEach(function (entry { myString += entry.product.name; } alert(myString); just build a string in the for loop then alert it var myString; forEach(function (entry { myString += entry.product.name; } alert(my ...
-
您使用的是ajax ,输入控件类型“ submit ”对于放置它的表单有一个默认的回发操作。 所以jQuery和大多数javascript代码都使用evt.preventDefault(); // evt is the object of event passed as a parameter for the click event function evt.preventDefault(); // evt is the object of event passed as a parameter for t ...
-
addItem()的代码asynchronously向资源发出GET请求。 这意味着执行此函数之后的代码将不会等待其执行完成。 当我取消注释警报时,它可以正常工作。 这是因为当内置alert()时,它会暂停脚本的执行,直到用户交互。 这给了addItem()需要的时间,一切似乎都有效。 幸运的是,有解决方案可以解决这种情况。 诺言。 重新排列代码以使用回调。 在封面下, $.getJSON是使用带有datatype = 'json' ajax进行GET请求的简写,它返回一个promise对象,它基本上告诉我 ...
-
alert(one())提醒函数one的返回值。 该函数不返回值 ,因此其返回值undefined 。 如果要更改第二个alert的值,则需要在one alert中使用return
。 var n = 1; // global scope function one() { alert(n); return 42; } alert(one()); alert(one()) alerts the return value of the function one. Tha ...