Is there a way to simulate a click on an alert in JavaScript?

I have a iframe page whose source page is in a separate domain. From time to time, a warning is generated on the original page. When he does this, he stops what he is doing until the user clicks OK on the warning.

What I would like to do is programmatically click OK in this warning so that the original page can return to usefulness. Is it possible?

+7
javascript
source share
6 answers

JavaScript is single-threaded, which means that when a function is called, it is blocked until it returns. When you call alert (), this transfers control to the browser, which decides how to handle it. This is not Javascript that opens the user interface dialog; it is a browser. alert () does not return until the browser receives an OK event and returns control. The javascript thread stops until this happens.

So, for at least two reasons mentioned in the previous paragraph, the answer is no :)

+16
source share

I am sure this is not possible.

+7
source share

You should not have a problem if you know the text of the warning and do not mind getting infected.

You can override the iframe window warning with your own and do something as possible.

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

Edit: I did a little test with a proxy handler (it is in asp.net/c#, but you should get an idea) ...

The external page in my test is the w3 page with a button that displays a warning, the warning is now passed through a user-defined function.

I'm sure this is a lot dirtier than you would like to get.

Page:

 <%@ 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> 

Proxy Handler:

 <%@ 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; } } } 
+3
source share

What happens if you override the warning with no-op in your code?

eg.

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

When a line is commented out, a warning appears. When a line is commented out, a warning does not appear.

+2
source share

I really doubt. If for a separate domain you mean one that you do not have?

If you have control, you can change the warnings to Javascript modal blocks that you could control.

+1
source share

The best solution to solve your annoying JavaScript problems would be to override it with a client-side scripting script like Greasemonkey .

0
source share

All Articles