Welcome Guest Search | Active Topics | Sign In | Register

After web Browser is closed and reopen - javascript functions are not found Options
Noa
Posted: Monday, March 28, 2022 9:29:55 AM
Rank: Newbie
Groups: Member

Joined: 3/28/2022
Posts: 4
Hello,
on my project, I open WPF window with EO WebBrowser control - on this control I set the url to html page.

<Window x:Class="Astea.AddressMaps.AddressMap"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:eo="http://schemas.essentialobjects.com/wpf/"
Title="AddressMap" Height="650" Width="1200">
<eo:WebControl>
<eo:WebControl.WebView>
<eo:WebView x:Name="mainWebView">
</eo:WebView>
</eo:WebControl.WebView>
</eo:WebControl>
</Window>

public AddressMap(string sessionID, string url)
{
_sessionId = sessionID;
InitializeComponent();
InitializeDialogWindow();
mainWebView.LoadCompleted += mainWebView_LoadCompleted;
mainWebView.JSExtInvoke += new JSExtInvokeHandler(WebView_JSExtInvoke);
mainWebView.Url = url;
mainWebView.Closed += mainWebView_Closed;

}
void mainWebView_LoadCompleted(object sender, EO.WebBrowser.LoadCompletedEventArgs e)
{
var Jobj = mainWebView.GetDOMWindow();
Jobj.InvokeFunction("MapAddressLoad", _sessionId);

}
Behind this html page I have javascript code.
on LoadCompleted event I call to function "MapAddressLoad" (defined in js) by invokeFunction.
In first time I open the wpf window - evrything is working OK and function is invoked correctly. but when I close the window and reopen it I get the error:
http://dsev2ctxastea27.il.astea.org/AsteaAllianceCurrent/App_includes/google_address_finder.js, line 44, col 4 - 5: Uncaught ReferenceError: SetGlobalVar is not defined
at System.Environment.GetStackTrace(Exception e, Boolean needFileInfo)
at System.Environment.get_StackTrace()
at EO.Base.BaseException..ctor(String lsy, Exception lsz)
at EO.WebBrowser.JSInvokeException..ctor(JSInvokeResult wx, String wy)
at EO.WebBrowser.JSException..ctor(String xa, Int32 xb, Int32 xc, Int32 xd, String xe)
at EO.WebBrowser.JSException.xmbk(yrnx wz)
at EO.WebBrowser.WebView.alzf(String go, JSInvokeResult gp, yrnx gq, Exception& gr)
at EO.WebBrowser.ScriptCall.dcpz(JSInvokeResult ye, qscn yf, String yg)
at EO.WebBrowser.ScriptCall.doyx.ymgi(qscn yj)
at EO.Internal.qsge`3.frre.orca(m ope)
at EO.Internal.qsge`3.nrxm(m onx)
at EO.Internal.yrni.dkho(Object gc)
at EO.Internal.qsge`3.nrxl()
at EO.Internal.qsge`3.nrxk(IntPtr onv, Int32 onw)
at EO.Internal.qsge`3.frrd.xlcv(qsao oot)
at EO.Internal.qsge`3.frrd.mkdt(Object oos)
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart(Object obj)

SetGlobalVar is called from invoked function MapAddressLoad and it seems that after the window is closed and reopened this function is not found anymore.
Do you have an idea why?
eo_support
Posted: Monday, March 28, 2022 2:02:51 PM
Rank: Administration
Groups: Administration

Joined: 5/27/2007
Posts: 24,217
What version of our DLLs are you using?
Noa
Posted: Tuesday, March 29, 2022 7:10:10 AM
Rank: Newbie
Groups: Member

Joined: 3/28/2022
Posts: 4
version 22.0.40.0
eo_support
Posted: Tuesday, March 29, 2022 11:17:34 AM
Rank: Administration
Groups: Administration

Joined: 5/27/2007
Posts: 24,217
Hi,

This could be a timing issue where your code has been called before you calling Jobj.InvokeFunction inside mainWebView_LoadCompleted. This is because mainWebView_LoadCompleted is NOT synchronous. When the page finishes loading inside the browser engine, it sends a notification to the .NET side notifying the page has finished loading, at the same time the JavaScript code in the page continuous to run. If there is some code in the JavaScript code that checks the variable at this point and your .NET side mainWebView_LoadCompleted didn't get a chance to run yet, you can have this problem.

The easiest way to avoid such problem is to use WebView.JSInitCode. This code is guaranteed to run before anything else inside the page. So you can pass your sessionId or set up document.load event handler through here. This way your sessionId will be guaranteed available when other code runs in your page.

Thanks!
Noa
Posted: Wednesday, March 30, 2022 3:01:41 AM
Rank: Newbie
Groups: Member

Joined: 3/28/2022
Posts: 4
Hi
Thanks for your answer.
Can you send me an example of how should I use this property JSInitCode?
Should I use it instead of call mainWebView_LoadCompleted event?
What I need is that when the page is load- before we get the document.ready function on JavaScript- I need to set the session ID.
I created function on JS named "MapAddressLoad" and she get the session ID and set the global parameter of sessionID on javaScript.
How exactly should I call this function with this property? I didn't find examples of using this property.
eo_support
Posted: Thursday, March 31, 2022 5:42:56 PM
Rank: Administration
Groups: Administration

Joined: 5/27/2007
Posts: 24,217
Hi,

This would depend on what you want to do. For example, if you want to make sure a global variable _sessionId is defined before your document.ready is called, you can do:

Code: C#
webView.JSInitCode = string.Format("window[' _sessionId'] = '{0}'", sessionId);


If your sessionId is "1234", then this would be equivalent to running the following JavaScript code:

Code: JavaScript
window["_sessionId"] = "1234";


What's special aboubt JSInitCode is that the code you passed in is guaranteed to run BEFORE any other code in your page runs. Beside that it's just plain JavaScript code.

Hope this helps.

Thanks!
Noa
Posted: Sunday, April 3, 2022 3:15:49 AM
Rank: Newbie
Groups: Member

Joined: 3/28/2022
Posts: 4
Hi Thanks,
Can I call a function that is defined on js code with this property?
How can I do it? I have several code lines that I need to do before document.ready so I defined a function in js.
eo_support
Posted: Monday, April 4, 2022 9:58:00 AM
Rank: Administration
Groups: Administration

Joined: 5/27/2007
Posts: 24,217
Noa wrote:
Hi Thanks,
Can I call a function that is defined on js code with this property?


You can define your function in JSInitCode. For example:

Code: C#
webView.JSInitCode = "function Hello() { your code here... };";


Then this function will be avaliable for any other code in the page.

There is NOTHING particular about JSInitCode except that it runs BEFORE any other code in your page. This is the entire point of JSInitCode. Beside that you can put whatever JavaScript code there.

Thanks!




You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.