Welcome Guest Search | Active Topics | Sign In | Register

Assign onload javascript dynamically Options
Christian Porzio
Posted: Wednesday, June 28, 2017 6:51:35 PM
Rank: Advanced Member
Groups: Member

Joined: 10/4/2016
Posts: 104
Hi,

I have a web page with a iFrame. I do not own the host page but I need to assign the host web page title to the same value as the title of that iFrame each time it is has loaded.

When I create my own fake host page as follow:
Code: HTML/ASPX
<iframe src="https://prime.aa.com/prime/prime.html" name="hostedAppFrame" id="hostedAppFrame" onload="document.title = document.getElementById('hostedAppFrame').contentDocument.title;">
Browser does not support iFrame. Please contact your administrator.
</iframe>

It works just fine, however the real host page does not have a onload defined in its iframe element.

So in my code, when the host page has loaded, I'm trying to redefine dynamically the onload handler by executing the following script:

Code: C#
ExecuteScript("document.getElementById('hostedAppFrame').onload=\"document.title = document.getElementById('hostedAppFrame').contentDocument.title;\"", out res)


FYI... I execute this javascript using this code - it works very well for all the other scripts I execute.

Code: C#
public override bool ExecuteScript(string script, out object result)
        {
            result = null;
            object res = null;
            bool success = false;
            try
            {
                Application.Current.Dispatcher.Invoke(new Action(() =>
                {
                    lock(webView)
                    {
                        JSScriptHander jshdl = new JSScriptHander(script);
                        if (webView.CanEvalScript)
                        {
                            ScriptCall myCall = webView.QueueScriptCall(jshdl.Call);

                            if (myCall.WaitOne(1000))
                            {
                                if (!myCall.IsAborted && myCall.IsDone)
                                {
                                    res = myCall.Result;
                                    success = true;
                                }
                            }
                        }
                    }
                }));
            }
            catch (EO.WebBrowser.JSException jse) {
            }
            catch (Exception e) {
            }
            result = res;
            return success;
        }


At runtime, once the call has executed, result is an instance of a string of value: "document.title = document.getElementById('hostedAppFrame').contentDocument.title;"

So I assume it worked, however next time my iFrame changed, hence the onload event has been triggered, nothing happens.

I am almost positive my problem is not in the way I execute the javaScript but rather in a misunderstanding of how much I can actually re-assign dynamically event handlers such as the onload of a iFrame.

Any idea?

Thanks a lot in advance!

eo_support
Posted: Wednesday, June 28, 2017 7:46:03 PM
Rank: Administration
Groups: Administration

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

You should use this property instead:

https://www.essentialobjects.com/doc/eo.webbrowser.webview.jsinitcode.aspx

The JS code you set through this property will be executed inside the main frame and every iframe BEFORE any JavaScript code is run in that frame. You can hook up your onload handler there.

Thanks!
Christian Porzio
Posted: Thursday, June 29, 2017 4:52:16 PM
Rank: Advanced Member
Groups: Member

Joined: 10/4/2016
Posts: 104
As always thank you for your quick response.

In this case, the problem was not when I was calling the script but the script itself.

To assign the onload handler, the proper code was:
Code: JavaScript
document.getElementById('hostedAppFrame').onload = function() { return function() { document.title = document.getElementById('hostedAppFrame').contentDocument.title; } }()


That did the trick - again only if the engine is started with the native options: --disable-web-security --user-data-dir to allow the host page to query the iFrames.

eo_support
Posted: Friday, June 30, 2017 9:03:37 AM
Rank: Administration
Groups: Administration

Joined: 5/27/2007
Posts: 24,225
Great. Glad to hear that you worked it out!


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.