|
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.
|
|
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!
|
|
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
|
|
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; };".
|
|
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
|
|
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!
|
|
Rank: Member Groups: Member
Joined: 8/4/2015 Posts: 10
|
Thank You. I will check this and let you know.
|
|
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.
|
|
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!
|
|
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?
|
|
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!
|
|
Rank: Member Groups: Member
Joined: 8/4/2015 Posts: 10
|
Thank you so much. I have resolved the problem now in a different way :)
|
|