Welcome Guest Search | Active Topics | Sign In | Register

How to add a java script object for a callback Options
usyber
Posted: Wednesday, December 2, 2015 9:08:36 AM
Rank: Newbie
Groups: Member

Joined: 11/12/2015
Posts: 9
Dear Essential Team,

I'd like to add a java script object in the WebView component for a callback functionality which will be called when the user presses a cancel button on the page. Therefore I read the documentation (Using and Executing Java Script Code) and tried the following without success.

Code: C#
private void LoadPageWithListener()
        {
            //Java script listener object
            var jsListener= @"function JsListener() {
	                            this.Cancel = function () {
                                    jsCsCancelCalled();    		                        
	                            }	
                            }
                            var jsListenerObj = new JsListener();";

            //Register new C# callback method
            webView.RegisterJSExtensionFunction("jsCsCancelCalled", OnCancelListener);
            
            //Append java script listener
            webView.EvalScript(jsListener);

            var url = "server/frame?listener= jsListenerObj";

            //Load the URL request with the JsListener instance name as parameter 
            webView.LoadUrl(url);
        }

        private void OnCancelListener(object sender, JSExtInvokeArgs e)
        {
        }


The publisher uses the 'jsListenerObj' instance name from the request URL and calls the 'Cancel' function where my registered java script function is called.

How can I add a java script object for the callback function as described?

Best regards,
Uli

eo_support
Posted: Wednesday, December 2, 2015 9:35:26 AM
Rank: Administration
Groups: Administration

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

Your code is almost correct. The problem is when you call LoadUrl, a new page is loaded and everything about the old page is discarded --- including the jsListener JavaScript function that you injected to the previous page. In another word, jsListener would exists inside the page before LoadUrl occurs, but it does not exist in the new page after LoadUrl is called.

To solve this problem you can use WebView.JSInitCode. Any JavaScript code you set in this property will be automatically executed in every page before everything else. This way your jsListener function would exist in every page.

Thanks!
usyber
Posted: Wednesday, December 2, 2015 10:04:41 AM
Rank: Newbie
Groups: Member

Joined: 11/12/2015
Posts: 9
Dear Admin,

thank you very much for your quick response - it works now!!

greetz,
Uli
eo_support
Posted: Wednesday, December 2, 2015 10:08:14 AM
Rank: Administration
Groups: Administration

Joined: 5/27/2007
Posts: 24,229
Great! Please feel free to let us know if you have any other questions.

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.