ExecuteScript question

Ask general questions here.
User avatar
Aracknid
Posts: 342
Joined: Tue Aug 10, 2010 3:23 pm
Location: Toronto, Ontario, Canada

ExecuteScript question

Post by Aracknid » Tue May 16, 2017 7:43 pm

Hi,

I have a feature in my web app that can appear within its own Window (thus its own WebDocument) or within an IFrame of its parent window.

I've written a bunch of code in my framework to handle getting some variable info from my web app's WebDocument object using ExecuteScript. It works fine when its in its own WebDocument, but fails when embedded in the IFrame.

I realize why, because I'm using code like this:
StrValue = MyWebDocObj.ExecuteScript("return MyVar;")
And of course when embedded in the IFrame, it isn't looking at that.

So, I can also do this:
If Embedded then
    StrValue = MyIFrameObj.ExecuteScript("return MyVar;")
Else
    StrValue = MyWebDocObj.ExecuteScript("return MyVar;")
End if
But I have a lot of places with executescript calls, and what I'd really like to do is make it more generic so that it will work without a condition. Is it possible to do this?

For example:
StrValue = MyGeneralObj.ExecuteScript("return MyVar;")
Where MyGeneralObj works for either a WebDocument or an IFrame.

Thanks,

Aracknid

User avatar
RobinHood42
Posts: 239
Joined: Fri Jan 09, 2015 3:24 pm

Re: ExecuteScript question

Post by RobinHood42 » Wed May 17, 2017 11:27 am

Hi,

the easiest way would be to add a new class like:

Code: Select all

Public NotInheritable Class MyGeneralObj
	Private Sub New()
	End Sub
	Public Shared Function GetVar(Embedded As Boolean) As String
		If Embedded Then
			Return MyIFrameObj.ExecuteScript("return MyVar;")
		Else
			Return MyWebDocObj.ExecuteScript("return MyVar;")
		End If
	End Function
End Class
and replace all your calls to

Code: Select all

StrValue = MyWebDocObj.ExecuteScript("return MyVar;")
with

Code: Select all

StrValue = MyGeneralObj.GetVar(Embedded)
hth