471,310 Members | 1,702 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,310 software developers and data experts.

How can I call page function from automation code?

Hey all,

I am automating a web page from Visual Foxpro. I can control all the
textboxes, radio buttons, and command buttons using syntax such as:

oIE.Document.Forms("searchform").Item(<name>).Valu e = <myvalue>

But I cannot control a dropdown with an onchange event. I can set the
dropdown's value and selectedIndex, but then calling the onChange() or
Click() does not do anything. It only seems to fire the onchange if I
use it interactively.

I know what the onChange calls on the form, and I can call the
function directly, but I don't know how. How does one call a straight
JavaScript function that is in the script portion of the web page and
have it fire as if it was called from the page itself?

Just point me to any docs and I should be able to figure it out. I am
just missing this one step, but it is crucial for this manipulation...

You can email me at: junk (at) removethis.sutekh137.net, and I will
try to monitor the newsgroup.

Thanks in advance!
JoeK
Jul 23 '05 #1
3 3825
jo*@sutekh137.net (JoeK) wrote in message news:<47*************************@posting.google.c om>...
Hey all,

I am automating a web page from Visual Foxpro.
I do not know Visual Foxpro. Hope that my answer will still be on
track.

oIE.Document.Forms("searchform").Item(<name>).Valu e = <myvalue>

But I cannot control a dropdown with an onchange event. I can set the
dropdown's value and selectedIndex,
I assume you are setting these things from javascript.
but then calling the onChange() or
Click() does not do anything.
I asume you mean the event handlers are not called when you chanage
the values. This is true. They are not called. You need to call
the functions directly after you change the data in javascript.
It only seems to fire the onchange if I
use it interactively.
True. The event handlers response only to screen input.

I know what the onChange calls on the form, and I can call the
function directly, but I don't know how.
It is a standard call. The user of the this keyword makes the
function call looks special, but it is a standard function call. You
have to supply whatever the this would have supplied because you will
not be in the this scope chain. ( I do not think, but you can try.)
How does one call a straight
JavaScript function that is in the script portion of the web page and
have it fire as if it was called from the page itself?
Here is my file where I have one style of call with the this and
another without. It is probably best to first run this file. It will
explain more about what is going on.

Robert

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<HTML>
<HEAD>
<Title>My color test</Title>
<script type="text/javascript">

function processColors(form)
{
var box;
box = document.createElement("p");
document.body.appendChild(box);
box.innerHTML = "<p>Display all <b>three</b> inputs.</p>";

for (var i=1; i<=3; i++)
{

// Here I call the onchange function.
displayOneColor(form,"color"+i);
}

return true;
} //end function

function displayOneColor (form,theColor)
{
var colorList = ["green","blue","red","black"];
var boxColor;
var box;

boxColor=form.elements[theColor].value;
box = document.createElement("p");
document.body.appendChild(box);
box.innerHTML =
"<p><span style='color:" + colorList[boxColor] + "';>" +
"You picked color "+ colorList[boxColor] +
" for input " + theColor + "." +
"</span></p>";
}
function validateOnSubmit(form)
{
alert("in validateOnSubmit.");
}

</script>
</HEAD>
<BODY >
<p>This files show how you can call an onchange event
handler directly in the onchange event or indirectly from
another function. The
function displayOneColor is called from each of the onchange
event handler in the select tags. To see the direct call, look in the
for loop
of processColors.</p>
<p>Changing on of the colors in the three select box
will display a message at the bottom of the screen.
Clicking on the GO button will show all three color
selections. Clicking on Submit Query will erase all
the color messages since the file is being reloaded. </p>

<form name="form1"

Pick color 1:
<SELECT name="color1" name=color1 style="WIDTH: 180px"
onchange="displayOneColor(this.form,this.name);">
<OPTION value = "0" >Green
<OPTION value = "1" >Blue
<option value = "2" >Red
<option value = "3" >Black
</OPTION></SELECT>
<br><br>
Pick color 2:
<SELECT name="color2" style="WIDTH: 180px"
onchange="displayOneColor(this.form,this.name);">
<OPTION value = "0" >Green
<OPTION value = "1" >Blue
<option value = "2" >Red
<option value = "3" >Black
</OPTION></SELECT>
<br><br>
Pick color 3:
<SELECT name="color3" style="width: 180px"
onchange="displayOneColor(this.form,this.name);">
<OPTION value = "0" >Green
<OPTION value = "1" >Blue
<option value = "2" >Red
<option value = "3" >Black
</OPTION></SELECT>
<br>
<input TYPE=BUTTON NAME="cmdCalc" VALUE="GO"
onClick="processColors(this.form)">
<br><br>
<input type="submit">

</BODY>
</HTML>
Jul 23 '05 #2
JoeK wrote:
I am automating a web page from Visual Foxpro.
The odds are that there are not many (if any) regulars on this group who
have ever done that.
I can control all the textboxes, radio buttons,
and command buttons using syntax such as: oIE.Document.Forms("searchform").Item(<name>).Valu e = <myvalue>
That code strongly resembles VBScript (is it?). A javascript (or,
probably more correctly, JScript) version might go:-

oIE.document.forms["searchform"].elements[<name>].value = '<myvalue>';

And automating IE with WSH might acquire a reference to a new IE
application as:-

var oIE = new ActiveXObject("internetexplorer.application");

- but you haven't gone into how you are getting your - oIE - reference.
But I cannot control a dropdown with an onchange event. I can set the
dropdown's value and selectedIndex, but then calling the onChange() or
Click() does not do anything. It only seems to fire the onchange if I
use it interactively.
Browser form controls do not tend to fire change events when their
values are set through scripts. But scripts that set the values of
controls may call the corresponding onchange handlers themselves as
method of the form control. In JScript that would be:-

var frmEls = oIE.document.forms["searchform"].elements;
frmEls[<name>].onclick();
- or -
frmEls[<name>].click(); //for the - click - method.

(javascript/JScript/ECMAScript is case sensitive.)
I know what the onChange calls on the form, and I can call the
function directly, but I don't know how. How does one call a straight
JavaScript function that is in the script portion of the web page and
have it fire as if it was called from the page itself?

<snip>

In JScript, as above, but in whichever language you are using I don't
know. Though it will almost certainly follow a similar form/structure.

Richard.
Jul 23 '05 #3
> > How does one call a straight
JavaScript function that is in the script portion of the web page and
have it fire as if it was called from the page itself?


There is a more elegant way of calling the event handler. Since the
handler are functions, you can call the function directly. This is an
example:
// Call the event handler for the select
form.elements["color" + i].onchange();
"Note, however, that invoking an event handler is not a way to
simulate what happens when the event actually occurs. If we invoke
the onclick method of a Link object, for example, it does not make the
browser follow the link and load a new document." from Javascrip: The
Definitive Guide page 357.

Here is the revised function...

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<HTML>
<HEAD>
<Title>My color test</Title>
<script type="text/javascript">

function processColors(form)
{
var box;
box = document.createElement("p");
document.body.appendChild(box);
box.innerHTML = "<p>Display all <b>three</b> inputs.</p>";

for (var i=1; i<=3; i++)
{

// Here I call the onchange function.
// The commented line directly invokes the underlaying
// function.
// displayOneColor(form,"color"+i);

// Call the event handler for the select
form.elements["color" + i].onchange();
}

return true;
} //end function

function displayOneColor (form,theColor)
{
var colorList = ["green","blue","red","black"];
var boxColor;
var box;

boxColor=form.elements[theColor].value;
box = document.createElement("p");
document.body.appendChild(box);
box.innerHTML =
"<p><span style='color:" + colorList[boxColor] + "';>" +
"You picked color "+ colorList[boxColor] +
" for input " + theColor + "." +
"</span></p>";
}
function validateOnSubmit(form)
{
alert("in validateOnSubmit.");
}

</script>
</HEAD>
<BODY >
<p>This files show how you can call an onchange event
handler directly in the onchange event or indirectly from
another function. The
function displayOneColor is called from each of the onchange
event handler in the select tags. To see the direct call, look in the
for loop
of processColors.</p>
<p>Changing on of the colors in the three select box
will display a message at the bottom of the screen.
Clicking on the GO button will show all three color
selections. Clicking on Submit Query will erase all
the color messages since the file is being reloaded. </p>

<form name="form1"

Pick color 1:
<SELECT name="color1" name=color1 style="WIDTH: 180px"
onchange="displayOneColor(this.form,this.name);">
<OPTION value = "0" >Green
<OPTION value = "1" >Blue
<option value = "2" >Red
<option value = "3" >Black
</OPTION></SELECT>
<br><br>
Pick color 2:
<SELECT name="color2" style="WIDTH: 180px"
onchange="displayOneColor(this.form,this.name);">
<OPTION value = "0" >Green
<OPTION value = "1" >Blue
<option value = "2" >Red
<option value = "3" >Black
</OPTION></SELECT>
<br><br>
Pick color 3:
<SELECT name="color3" style="width: 180px"
onchange="displayOneColor(this.form,this.name);">
<OPTION value = "0" >Green
<OPTION value = "1" >Blue
<option value = "2" >Red
<option value = "3" >Black
</OPTION></SELECT>
<br>
<input TYPE=BUTTON NAME="cmdCalc" VALUE="GO"
onClick="processColors(this.form)">
<br><br>
<input type="submit">

</BODY>
</HTML>
I know the prior poster mentioned this. I thought I was invoking the
event function, but I was invoking my event function not the DOM event
function and I didn't know I could do it this way until today.

Robert

Robert
Jul 23 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Kannan | last post: by
7 posts views Thread by Gabe Covert | last post: by
4 posts views Thread by gs | last post: by
11 posts views Thread by yangsuli | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.