Answered by:
How to call javascript functions through VFP code

Question
-
Dear All,
I am working on web site automation in Visual foxpro. I have to call website Javascript fuctions through foxpro code. Please help me. I tried following but it is not working..
oBrowser = CREATEOBJECT("InternetExplorer.Application")
obrowser.Visible = .t.
oBrowser.Navigate("C:\temp\html\_preview.html")oBrowser.Document.parentWindow.showoverlay(.F.)I am not geeting parentwindow object... showing error.. i tried document.Script also no luck
Please help me to call JS function through foxpro code.
Thanks in advance
Regards
Ram
Tuesday, August 3, 2010 5:16 AM
Answers
-
Ram,
execScript is not mine. It is part of the parentWindow. I am using VFP 8. For my code I needed to set the required value (in your case windows.event.keycode value) before firing the function.
"how can i force this key also while call our execScript through code.. bez i am programatically fill the value...no keypress is happning.. JS function is below"
As I stated above, you don't have to worry about the keypress event. In VFP, you set the required value though code.
In my first post, the name of the value I need to set is "Pkgs".
I set the value to 99
oIE.document.frames('main').document.forms.item(0).elements(0).value = 99
Stepping through my code, I was able to see the web site change to the value I programmed(99 = Alphabetical).
once this value is set, then the website knows what this value is. Now all I need to do is call the JS function FillSelect().
In my first post, you can see that the onChange event fires the FillSelect() function. If you think outside the box, you will realize that you're not limited to calling FillSelect() only with the Pkgs.onChange. You can call this anytime while the page is open.
So what I am doing is just calling the FillSelect() function when I am ready for it. In my case, as soon as I call set the value for Pkgs, I can call the FillSelect() function on the very next line of code. The site does not care that I call it through any kind of onChange event.
From what I can see, you need to look at the function and see what variables need to be established prior to the Function call. Once these variables are established, then you should be able to call the JS function at any time.
I have not used this approach for jquery, yet.
- Marked as answer by ramjai Monday, August 9, 2010 10:34 AM
Thursday, August 5, 2010 2:57 PM
All replies
-
Check Below Link. there is Example how to Call JavaScript from VFP
http://www.tek-tips.com/faqs.cfm?fid=4257
Please "Mark as Answer" if this post answered your question. :)
Kalpesh Chhatrala | Software Developer | Rajkot | India
Kalpesh 's BlogTuesday, August 3, 2010 6:18 AMAnswerer -
Dear Kalpesh,
Thanks a lot for your reply. I tried that sample. This is for creating javascript and run throug our code.My actual problem is i am doing automation of web site, automatically i loged web site after that web site having javascript functions, i want to call that javascript functions through IE object.First i am creating IE object .. then login and next navigate the second page.. that page having some scripts that i want to call... please help me..
Regards
Ram
Tuesday, August 3, 2010 6:33 AM -
You are trying to navigate to local page that use file: protocol. Try to test it on web-server.
Also try
oWebEdit.submit oBrowser.sync oWebEdit.init
Custom Development
Placing a link in a sig is a common practice novadays.- Proposed as answer by greenwichclc Wednesday, August 4, 2010 4:54 PM
Wednesday, August 4, 2010 11:34 AM -
I am looking at the same issue. I am using VFP to connect to a website. the site has the following
<TD Align="Left" valign="bottom" Class="cssNormalTxt" width="50%">
<SELECT Name="Pkgs" id="Pkgs" onChange="FillSelect()"><option value="100">Select a Category </option><option value="99">Alphabetical</option><option value="12">Behavioral</option><option value="13">Training</option></SELECT>
</TD>I can change the dropdown value
oIE.document.frames('main').document.forms.item(0).elements(0).value = 99
What needs to happen next is the onChange event = FillSelect() has to fire and populate the list.
I have been tooling around with Scripts
lcScripts = oIE.document.frames('main').document.Scripts
lcScripts.length = 4
Iterating through the lsScripts items,lcScripts(0).text, reveals all functions on the page. However, lcScripts(2).text and lcScripts(3).text each have multiple functions inside the text. The FillSelect() function exists in lcScripts(2).text. So I know that VFP has the ability to see the JS functions.
I had also been playing with this:
oPage = oIE.document.frames('main').document
oPage.parentWindow.execScript(code as string, lang as string)
But to no avail.
In writing this post, I tried it again
oIE.document.frames('main').document.forms.item(0).elements(0).value = 99
oPage.parentWindow.execScript("FillSelect()", "JavaScript")
And voila! the list box populated with my selections based on the drop down list.
YES!
So for me, I changed the element value, then executed the execScript.
I have 2 more functions that I need to work next, with parameters being passed to the function. I will let you know how it works out.
Wednesday, August 4, 2010 4:55 PM -
for the JS function that required parameters
<td Align="center" valign="bottom" Class="cssNormalTxt">
<SELECT name="TestList" size="7" style="width: 98%" onDblClick="selMove(document.form1.TestList,document.form1.Selected,false);"><!-- OnChange="GetTestID(this)"-->
</select>IN VFP, I set the value for TestList then
oPage.parentWindow.execScript("selMove(document.form1.TestList,document.form1.Selected,false)", "JavaScript")
Notice I did NOT have to call any onclick or onDlbClick events in VFP. Also take note that there were no VFP variables passed using .execScript
Gorgeous!
- Edited by greenwichclc Wednesday, August 4, 2010 5:35 PM spelling
Wednesday, August 4, 2010 5:06 PM -
Hi Greenwichclc,
Firstly thanks a ton for your helful reply. I ran your execScript in VFP7 it is not showing any error.. but also no output may be becz of that website function having window.event.keyCode == 40 and window.event.keyCode == 13 (for enter and downarrow).. how can i force this key also while call our execScript through code.. bez i am programatically fill the value...no keypress is happning.. JS function is below
function txtFocus(txtType, indPrev, indNext) {
try {
if (txtType == "UP") {
if(window.event.keyCode == 40 || window.event.keyCode == 13){
var obj = document.getElementById(txtUnitPriceFields[indNext + 1]);
obj.focus();
}
if(window.event.keyCode == 38){
var obj = document.getElementById(txtUnitPriceFields[indPrev +1]);
obj.focus();
}
} }
catch(ex){} }another one help in that web page triggering jquery also while that textbox value changed.. how can i do that.. i am placing jquery first 5 lines.. for your kind reference...
for element name getting
var tbUnitPrice = $('input[type="text"]').filter(function (index) { return $(this).attr("id").indexOf("txtUnitprice") != -1 || $(this).attr("id").indexOf("txtDiscount") != -1});
jquery change event trigger...
$(tbUnitPrice).change(function()
{
......codes...}
i want trgger this also in same time....
Please help me and i am wating for your reply......
Regards
Ram
Thursday, August 5, 2010 4:53 AM -
Ram,
execScript is not mine. It is part of the parentWindow. I am using VFP 8. For my code I needed to set the required value (in your case windows.event.keycode value) before firing the function.
"how can i force this key also while call our execScript through code.. bez i am programatically fill the value...no keypress is happning.. JS function is below"
As I stated above, you don't have to worry about the keypress event. In VFP, you set the required value though code.
In my first post, the name of the value I need to set is "Pkgs".
I set the value to 99
oIE.document.frames('main').document.forms.item(0).elements(0).value = 99
Stepping through my code, I was able to see the web site change to the value I programmed(99 = Alphabetical).
once this value is set, then the website knows what this value is. Now all I need to do is call the JS function FillSelect().
In my first post, you can see that the onChange event fires the FillSelect() function. If you think outside the box, you will realize that you're not limited to calling FillSelect() only with the Pkgs.onChange. You can call this anytime while the page is open.
So what I am doing is just calling the FillSelect() function when I am ready for it. In my case, as soon as I call set the value for Pkgs, I can call the FillSelect() function on the very next line of code. The site does not care that I call it through any kind of onChange event.
From what I can see, you need to look at the function and see what variables need to be established prior to the Function call. Once these variables are established, then you should be able to call the JS function at any time.
I have not used this approach for jquery, yet.
- Marked as answer by ramjai Monday, August 9, 2010 10:34 AM
Thursday, August 5, 2010 2:57 PM -
Hi Greenwichclc,
Thanks for your detailed reply.yes what you are saying is correct, today i have passed "windows.event.keycode = 40" below
.execScript("windows.event.keycode = 40", "JavaScript") but its showing window.event not found like that.. any other way is there to assign windows.event.keycode value.You replied you are not using Jquery... but is it possible to trigger JQuery events ? i have searched more sites.. but no idea...Please help me .. if you have any idea...waiting for your reply...
Regards
RAM
Friday, August 6, 2010 10:37 AM -
Hi Greenwichclc,
I have opend another post for Jquery.. below is link... http://social.msdn.microsoft.com/Forums/en/visualfoxprogeneral/thread/bfba8cc2-9fce-4990-baec-ab423f7108b3
Please help to call Jquery event through VFP.
Regards
RAM
Monday, August 9, 2010 10:34 AM -
Hey Ram!
I have discovered a successful trick: Insert a link into the html document whose onclick event fires the desired code.
I was unable to trigger the "onchange" code associated with an html SELECT element:
<B xmlns:msxml="urn:schema-microsoft-com:xslt">Columns: </B>
<SELECT id=selGrdList class=Text_Small onchange=GrdSelected(this.value) xmlns:msxml="urn:schema-microsoft-com:xslt">
<OPTION value=473600>choice a</OPTION>
<OPTION selected value=360030>choice b</OPTION>
<OPTION value=457578>choice c</OPTION>
<OPTION value=316600>choice d</OPTION>
<OPTION value=321131>choice e</OPTION>
</SELECT>I set tn as an object in VFP:
tn=oIE.Document.getelementbyid("selGrdList")
tn.previousSibling.outerhtml="<a onclick='GrdSelected(457578)'>Columns:</a>"
tn.previousSibling.click
... and away fired the desired Javascript!
Monday, August 30, 2010 8:30 PM