Welcome Guest Search | Active Topics | Sign In | Register

Best Place to innject some JS Files Options
Nitesh
Posted: Monday, August 10, 2015 4:28:44 AM
Rank: Member
Groups: Member

Joined: 8/4/2015
Posts: 10
I would like to know the best place to inject some external JavaScript functions on a webpage.

I have tried loading them in WebView_LoadCompleted event but I notice on browsing a website's internal pages, in many instances this event is not fired at all.

Please suggest other alternatives.
eo_support
Posted: Monday, August 10, 2015 1:55:43 PM
Rank: Administration
Groups: Administration

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

You can try WebView.JSInitCode. JavaScript code included in this property are run for every frame before everything else.

Thanks!
Nitesh
Posted: Monday, August 10, 2015 3:10:09 PM
Rank: Member
Groups: Member

Joined: 8/4/2015
Posts: 10
Hi,
I need to run JS code after the web page has completed execution. Something like $(document).ready() in jQuery. Is it possible using the above code?

Regards,
Nitesh
eo_support
Posted: Monday, August 10, 2015 3:14:33 PM
Rank: Administration
Groups: Administration

Joined: 5/27/2007
Posts: 24,229
Then you can just set it to something like "window.onload = function() { your_code; };".
Nitesh
Posted: Monday, August 10, 2015 3:16:25 PM
Rank: Member
Groups: Member

Joined: 8/4/2015
Posts: 10
Using WebView.JSInitCode or somewhere else?

I tried in WebView_LoadCompleted but the event is not firing for all pages. Any other event handler where I can inject JS?

Regards,
Nitesh
eo_support
Posted: Monday, August 10, 2015 5:35:58 PM
Rank: Administration
Groups: Administration

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

WebView.JSInitCode is the place for you to inject JS code. WebView_LoadCompleted event will be fired for all top frames in the latest build. However in early build it will only be fired for request explicitly loaded by your code (through setting Url or LoadUrl/LoadRequest).

Thanks!
Nitesh
Posted: Tuesday, August 11, 2015 3:50:50 AM
Rank: Member
Groups: Member

Joined: 8/4/2015
Posts: 10
Thank You. I will check this and let you know.
Nitesh
Posted: Monday, August 17, 2015 3:59:27 AM
Rank: Member
Groups: Member

Joined: 8/4/2015
Posts: 10
Hello, I have checked this and find that if I set some JS code using JSInitCode, it gets loaded as soon as the page starts loading and does not wait for the entire document to load.

Neither window.load nor $(document).ready() events are working when defined in the JSInitCode. I am using the below code inside AttachPage() of TabbedBrowser sample -
page.WebView.JSInitCode = "var imp1 = 1; var imp2 = 2;" + File.ReadAllText("js/test.js");

test.js file contains a simple jquery plugin that should fire after page has loaded. Please advise.
eo_support
Posted: Monday, August 17, 2015 3:51:05 PM
Rank: Administration
Groups: Administration

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

Have you tried window.onload? That event should work fine. The behavior that JSInitCode is called before everything esle is by design ---- so that you can hook in whatever you want to hook in as early as possible. For example, if you want to extend the JavaScript runtime to contain your own "native" objects, this would be the moment to do so. You can of course also use any of the native DOM event such as window.onload to hook up your event handlers.

$(document).ready will not work because that relies on JQuery and at that point JQuery is not loaded yet.

Thanks!
Nitesh
Posted: Tuesday, August 18, 2015 3:54:12 AM
Rank: Member
Groups: Member

Joined: 8/4/2015
Posts: 10
It works. Thank you.

However, I notice that if there are some Javascript errors present on the webpage I am browsing to, my JavaScript execution fails. Are there any workarounds for this?
eo_support
Posted: Tuesday, August 18, 2015 6:05:20 PM
Rank: Administration
Groups: Administration

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

No. You will need to handle the JavaScript code error properly. EO.WebBrowser can not alter the JavaScript engine's behavior.

Thanks!
Nitesh
Posted: Wednesday, August 19, 2015 1:21:17 AM
Rank: Member
Groups: Member

Joined: 8/4/2015
Posts: 10
Thank you so much. I have resolved the problem now in a different way :)


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.