Link to home
Start Free TrialLog in
Avatar of SeMack
SeMack

asked on

How do I clear a JAVASCRIPT Alert message box in the InternetExplorer.Application Object?

Set IE = CreateObject("InternetExplorer.Application")

I am reletively new to using InternetExplorer.Application Object, and the VB language.

I am attempting to use VB to login to a website.  If a user enters an invalid username or password the page shows a JAVASCRIPT Window.Alert message.  I have attempted to suppress all popup messages with the IE.Silent = True property.  However, this does not stop the alert in question from being displayed.

Is there a way that I can access this Alert message box and either close it or click on the OK button?
Avatar of CyrexCore2k
CyrexCore2k
Flag of United States of America image

I know this isn't what you asked but if you download the source of the HTML page you are logging into you can take out all the javascript. Then have IE point to the local HTML document with no alert boxes.
Avatar of SeMack
SeMack

ASKER

Is there code that I can add to switch javascript off?
I don't believe there is. My suggestion was merely a work around if you can't find anything.


Spencer Ruport
You could try this...
Put a webbrowser (Webbrowser1) and an Internet Transfer Control (Inet1) on a form, and add this code, which comments out the alerts

Private Sub Form_Load()
WebBrowser1.Navigate ("about:blank")
End Sub

Private Sub WebBrowser1_DocumentComplete(ByVal pDisp As Object, URL As Variant)
myhtm = Inet1.OpenURL("http://Wherever")
With WebBrowser1.Document
.open
.write Replace(myhtm, "alert('", "//alert('")
.Close
End With
End Sub

Kindest regards,
Rhaedes
Avatar of SeMack

ASKER

I appreciate the comments.  However, I fear that they will not help me, and my original question still stands.

How do I gain access to a javascript window.alert message and click on the ok button?
Avatar of SeMack

ASKER

There have been no comments for a few days.  As a result I am raising the anti by a couple hundred points.  I realy need an answer to this question.

Some usefull things that I have learned.
1.  I was able to capture a collection of all open windows with the statement:

Set OpenWindows = New SHDocVw.ShellWindows

The alert window is not a separate window in this collection.

2.  I have been able to recreate the alert window myself with the command:

IE.Document.Frames.Window.Alert ("Howdy")

Is it possible that I can access the alert window somewhere in IE.Document.Frames.Window?

I appreciate the previous comments, but I do not think that I will be able to change the web page script.  I do not maintain the page, i am simply trying to use it.  

Additionaly, I need the information provided by the alert windows.  I need to capture the information provied, and I then need to clear the message and go on with the program.

Thank you.
Okay, well here's a thought to see if we can make any progress on this…
I stuck a webbrowser, a timer, a label and a commandbutton on a form. I set things up so that the timer changed the caption of the label to RND every 5 seconds, and so that when I clicked on the command button, the program fed the webbrowser some script to make an alert appear.
What I confirmed was that despite the fact that what goes on in the webbrowser container is to a large degree independent of the VB, the program waits for the user to click 'okay' on the alert before proceeding. In other words, when there is no alert, the label changes every five seconds, but when an alert appears, the program waits for the user to do something.
…So the following occurs to me. Instead of having the timer update a label, it could write a random number into a file every five seconds. Of course, it would stop doing this when a script alert appears.
To make this useful, you could create a small, independent application (also written in VB) that is called (using shell) by your main program in a navigate event. This small program would check the RND value saved in the file (say) every 10 seconds. If it finds the same value twice in a row, you know that the webbrowser has produced an alert. So you make it send a click to the focused default button on the alert.
As for getting the content of the alert message itself, I really have no idea (at present) of how to go about this. (As a first approximation I suppose you _could_ get your secondary app to take a snapshot of the screen before sending the carriage return to the system, but this obviously is not very satisfactory.)
Like I say, this is just an idea that I am sure can be improved upon.

Kindest regards,
Rhaedes
Avatar of SeMack

ASKER

Excelent Comment, Thank you.  I would like a more direct solution; I will give you the points if I don't get one.  I have noticed that my program waits for completion of the Alert window when I create one, but when it is generated by the webpage I am working with, my program does not wait.  IE.Busy will be True, and IE.ReadyState is either 3 or 4, so I can watch for it to timeout in one of these states.

OK, here is where I'm at right now.  In order to get a working version of this program to show my boss, I tried a workaround based on your comment 2/21/03.  I've implemented an AppActivate/SendKeys combo.  However, I called SendKeys and nothing happened.  I did some digging, and I found a function on a code bank refered to by rkot2000 in Visual_Basic/Q_20283694.html:
http://www.vb2themax.com/Item.asp?PageID=CodeBank&ID=198
A function named VbSendKeys by Bryan Wolf, 1999.  Hence, I wait for a timeout and then:

AppActivate (IE.Document.Title)
VbSendKeys ("{ENTER}")

AppActivate does not always work.  Sometimes I get the message "Invalid procedure call or argument".  If i alt+tab to the IE window and then continue the program the AppActivate works again.  Any Suggestions?
That's odd.
Presumably you get the error because the title property does not return the right value.
Have you tried a msgbox to show the title of the webpage, and compared it with what you get with a Ctrl-Alt-Del? (As I'm sure you're aware, only the beginning has to be the same.)

Perhps you could try using a TitleChange event to keep track of the title of the webpage and store it in a variable, and then use this in the AppActivate command.

Alternatively, perhaps you could loop through all the open applications to look for one containing 'Microsoft Internet Explorer', and activate that.

Otherwise, I'm out of suggestions! But wish you luck!

Kindest regards,
Rhaedes
Avatar of SeMack

ASKER

I did try that.  Acutally I have canned the AppActivate function; it was too unreliable in this case.  I am now relying on IE.Visible = True to bring my window into focus.

I guess all I need now is to be able to get the messages from the alert windows before I close them.
ASKER CERTIFIED SOLUTION
Avatar of Rhaedes
Rhaedes

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of SeMack

ASKER

Thank you.  This solution is almost perfect.  

This works exactly like I need it to with the test page, but I am having dificulty getting it to work with the live page.  I've changed the test page to look more like the function in the live page.  Still it works with the test page and not the live page.  I've spoken with the tech who maintains the page; he tells me that there are cold-fusion tags in the function.  Would this be causing the problem?  Does it matter that the script I am working with is JAVASCRIPT?  If so, I wonder why it worked on the test page and not the live page.  The error message I am getting is: "Run-time error '-2147352319(80020101)':  Could not complete the operation due to error 80020101."  I think this is run-time error '91'. This is occuring on the line with the execScript command in it.  What do you think?

I'm getting closer; this is exciting.  Thank you again.

'Test Page

<html>
<head>

<script LANGUAGE=JAVASCRIPT TYPE="text/javascript" >
function  showAnAlert()  {  window.alert( 'Hello' );  }
</script>

</head>
<body><button onclick="showAnAlert()">click me</button></body>
</html>

'VB functions

Dim IE As SHDocVw.InternetExplorer
Dim PW As MSHTML.IHTMLWindow2

Sub Main()
  Set IE = New SHDocVw.InternetExplorer
  IE.Navigate ("c:\mytestpage.htm")
  Do While IE.Busy
  Loop
  Do While IE.ReadyState <> 4
  Loop
 
  ChangeScripts

End Sub

Sub ChangeScripts()
  Dim MyScript As String
  Set PW = IE.Document.parentWindow
  For x = 1 To IE.Document.scripts.Length
    MyScript = IE.Document.scripts.Item(x - 1).Text
    'Change the document title to nothing, and then to the alert string
    MyScript = Replace(MyScript, "window.alert(", "document.title=(")
    'Feed the updated function back into the document
    PW.execScript (MyScript)
  Next
End Sub
Is the live page public? Can you post the URL or the html of the live page? If you don't want to post it here, maybe you could send it to rhaedes@yahoo.com so I can have a look?
Just a thought: could it be a small problem with the replace function? Look carefully at the live page to see of there are any descrepancies with either capital letters or whitespace. Otherwise please send me the html somehow.

Kindest regards,
Rhaedes
Also: I am assuming the page is not 'secure' (https)
...I've looked into the error message and it seems that there is a pipeline error from an SQL 'SELECT' in the Coldfusion. As I say, I really need to see the page to help any further.

Kindest regards,
Rhaedes
I'd like to see it too if possible

checkerman_1985@yahoo.com

Spencer Ruport
Sorry Cyrex, I wasn't trying to close out other experts! But I'm sure you'll agree, little more could be done without seeing the page. Essentially, I think that the method didn't work because the Javascript functions in the page are contained within HTML comment markers <!-- --> to hide them from older browsers.
This was news to me, and I really wonder if it is a bug.
In other words, 'document.scripts' returns a collection of all the functions, but the strings include the HTML comment markers where they are present. Why? I think this is rather bizarre, since the comment markers are HTML, not script.
I think this is why an error is produced with the execScript statement, since in fact it is trying to feed some HTML with the script.
The solution (I think) is to replace '<!--' and '-->' with nothing before feeding the function back in.

Kindest regards,
Rhaedes
Avatar of SeMack

ASKER

Sorry Cyrex, I just didn't know what Rhaedes was looking for out of the whole page.  I have spoken with the Cold-Fusion guy in our IT department.  He tells me that the scripts are automatically generated by the compiler, he didn't even know why the "<!--" and "//-->" were included in the script.  When I remove them with the function below, the page behaves appropriately.  The only down side is that the scripts that are not included in the pages until the page is submitted will still run their popups.  However, If I run the ScriptChange function after a popup window has occured, the title of the page still changes.  I can then clear the window with the VBSendKeys function discussed above.

Thank you both for your comments.  Rhaedes gets the Scoobie Snacks and the points.

I have included in the following code samples: 1. An examle script from the live page verbatum.  2. The solution VB sub that is based on Rhaedes' comments above.

'Example Page Function

<script LANGUAGE=JAVASCRIPT TYPE="text/javascript">

<!--

function  _CF_checkPMFLogin(_CF_this)
  {
    if ( PMFLogin.USERNAME.value == '' )
      {
        window.alert( 'ALERT\n\nAccount ID is required.' );
        PMFLogin.USERNAME.focus();
        return(false);
      }
    if ( PMFLogin.PASSWORD.value == '' )
      {
        window.alert( 'ALERT\n\nPassword is required.' );
        PMFLogin.PASSWORD.focus();
        return(false);
      }
    return(true);
    return true;
  }

//-->

</script>

'Solution Function

Sub ChangeScripts()
  Dim MyScript As String
  Set PW = IE.Document.parentWindow
  For x = 1 To IE.Document.scripts.Length
    MyScript = IE.Document.scripts.Item(x - 1).Text
    MyScript = Replace(MyScript, "window.alert(", "document.title=(")
    MyScript = Replace(MyScript, "alert(", "document.title=(")
    If Not MyScript = IE.Document.scripts.Item(x - 1).Text Then
      MyScript = Replace(MyScript, "<!--", "")
      MyScript = Replace(MyScript, "//-->", "")
      PW.execScript (MyScript)
    End If
  Next
End Sub
Avatar of SeMack

ASKER

Cudos and Thanx.
-SeMack
Glad to see that by hook or by crook you got it working!
Thanks for the A.
Kindest regards,
Rhaedes