代码之家  ›  专栏  ›  技术社区  ›  moffdub

有没有一种方法可以在JavaScript中模拟点击警报?

  •  11
  • moffdub  · 技术社区  · 16 年前

    我有一个带有iframe的页面,其源页面位于单独的域中。源页面会不时生成警报。当它这样做时,它会停止正在做的事情,直到用户单击警报的“确定”。

    6 回复  |  直到 16 年前
        1
  •  17
  •   Rex M    16 年前

        2
  •  7
  •   fig    16 年前

    我很确定这是不可能的。

        3
  •  3
  •   Quintin Robinson    16 年前

    <script>
    // uncomment next line to swallow alerts
    //  function alert(nope) { return; }
    <script>
    <script>
      $(document).ready(function() { alert("Hello, world!"); });
    </script>
    

    删除该行后,将出现警报。如果行中有注释,则不会出现警报。

        4
  •  2
  •   Thomas L Holaday    16 年前

    如果你知道警报的文本,并且不介意弄脏,你应该不会有问题。

    您可以用自己的警报覆盖iframe窗口的警报,并做一些效果如下的事情。。

    document.getElementById("InnerFrameName").contentWindow.alert = function(){
        if (arguments[0].toLowerCase() == "innerframe alert text")
            return; //supress
        else
            alert(arguments[0]);
    };
    

    编辑:我用代理处理程序做了一个小测试(它在asp.net/c#中,但你应该能理解)。..

    我敢肯定,这比你想要的要脏得多。

    页码:

    <%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>
    
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title>Hello From MyPage</title>
        <script type="text/javascript">
            function frameLoaded() {
                document.getElementById("InnerFrameName").contentWindow.alert = function() {
                    if (arguments[0].toLowerCase() == "i am an alert box!!") {
                        alert("MESSAGES WOULD BE SUPRESSED: " + arguments[0]);
                        return; //supress
                    } else {
                        alert(arguments[0]);
                    }
                };
            }
            </script>
    </head>
    <body onload="alert('Hello from an unsupressed top level frame message!');">
        <form id="form1" runat="server">
                <div>
                    <h1>Internal Page (Top Level)</h1>
                    <hr />
                    <iframe id="InnerFrameName" onload="frameLoaded();" src="PageProxy.ashx?externalURI=http://www.w3schools.com/JS/tryit_view.asp?filename=tryjs_alert"
                        style="width: 800px; height: 600px;">Derp!...</iframe>
                </div>
        </form>
    </body>
    </html>
    

    <%@ WebHandler Language="C#" Class="PageProxy" %>
    
    using System;
    using System.Web;
    
    public class PageProxy : IHttpHandler {
    
        public void ProcessRequest (HttpContext context) {
                byte[] externalPage = new System.Net.WebClient().DownloadData(context.Request["externalURI"]);
                context.Response.OutputStream.Write(externalPage, 0, externalPage.Length);
                context.Response.End();
        }
    
        public bool IsReusable {
            get {
                return false;
            }
        }
    }
    
        5
  •  1
  •   alex    16 年前

    我对此深表怀疑。如果说独立域名,你是指一个你无法控制的域名吗?

        6
  •  0
  •   William Clemens    16 年前

    解决恼人的JavaScript问题的最好办法是用客户端脚本工具覆盖它,比如 Greasemonkey .