Winforms web browser control not firing document complete with AJAX web site - html

The VB.Net desktop app uses the IE browser control to navigate the web. When a normal page loads the document_complete event fires and I can read the resulting page and go from there. The issue I am having is that the page I am driving is written with AJAX, so the document complete event never fires. Furthermore, when you view the source of the page after it loaded a new portion via AJAX, it hasn't change. How are people handling this? What are my options?

This solution might solve your problem.
prerequists:
AxwebBrowser control,
reference to mshtml.dll
Dim axmshtml As mshtml.HTMLDocument = YourAxWebBrowserControl.Document
Dim HTMLSource As String = axmshtml.body.innerHTML 'html source, including DOM changes
If you know what you are looking for you can put the above code in a timer/loop
and simply monitor the page source for changes.

If wb is your webbrowser control, then instead of getting the HTML by using:
wb.DocumentText
use:
wb.Document.Body.InnerHtml
This will give you the updated html, reflecting the AJAX update.
As to detecting when the AJAX completes, for me it seems to be triggering a DocumentCompleted event. Not sure why it's different for you.

You need to interact with the Javascript code in the website using the methods on HtmlDocument.

I have seen this kind of behavior with C# when some AJAX scripts created a race condition. Adding the defer attribute to the script tag helped in that case. YMMV.

Not sure if this will work.
When the Ajax call completes, add a random anchor hash to the URL like so: foo.html#23234
then add your code to the NavigateComplete2 event.
http://msdn.microsoft.com/en-us/library/aa768334%28VS.85%29.aspx

I'm guessing that the page your load in your windows app does an AJAX call, which appears to refresh the page. In that case, the document_complete event isn't fired, because the webpage itself isn't refreshed, but a portion of the page.
I found a similar question about this problem, with an accepted answer in VB.Net.

You can use the ProgressChanged event, it seems to fire during ajax calls

Related

How to reload an MVC page?

This is an MVC razor page, that works fine on first load.
If I use a reload from javascript or even a refresh in the browser menu (Chrome), I get
Server Error in '/' Application.
>The view 'actionChangeUserSentence' or its master was not found or no view
>engine supports the searched locations. The following locations were searched:
>~/Views/Conduct/actionChangeUserSentence.aspx
>~/Views/Conduct/actionChangeUserSentence.ascx
>~/Views/Shared/actionChangeUserSentence.aspx
>~/Views/Shared/actionChangeUserSentence.ascx
>~/Views/Conduct/actionChangeUserSentence.cshtml
>~/Views/Conduct/actionChangeUserSentence.vbhtml
>~/Views/Shared/actionChangeUserSentence.cshtml
>~/Views/Shared/actionChangeUserSentence.vbhtml
> and so on...
meaning the url string is considered a non MVC url.
I tried several possibilities to reload, including:
-location.reload();
-window.location.reload(false); with both false and true
-location.href = location.href;
-history.go(0);
By the way, the reason I need to refresh is that js drawing on a html canvas doesn't work except after full page (re)load, reason still unknown. But anyway, a browser menu refresh should of course always work, I guess...
Thanks for any help.
I will suggest put that code in on ready function of drawing canvas may this will help you
Use just an Response.Redirect("/<your page>");

Dynamically load an entire angular app

So I have a curious situation and I don't think it's going to work, but I figured I'd ask in case it is and someone knows how to. I am using a 3rd party website to create marketing funnels. You can add your own custom html and javascript, but it parses out the html in a rather unfavorable manor. Basically you specify an element on the page and it appends it as a data attribute and dynamically loads it into the DOM. Since this is happening this way, my app isn't being initialized because it's not in the DOM on page load. Is there a way to make this work? I'll explain a little deeper my configuration.
I add custom html such as:
<div data-ng-app="AppName"><div data-ng-controller="ControllerName"><div>perform controller logic here</div></div>
As you can see, this needs to be in the DOM for the app to initialize and, well work. Is there a way to initialize the app after the page has loaded dynamically? I can add my own JS files in the custom html field, but thats about as far as I can go for customization. Any help is appreciated!
Maybe you should execute angular's bootstrap function manually in your script after the required dom loaded.
var app = angular.module('appName', []);
app.controller([...]);
angular.bootstrap(document.getElementById('divId'), ['appName']);
For more information, you can see this doc https://docs.angularjs.org/api/ng/function/angular.bootstrap

How to Modify AJAX Based site Content OnDocumentComplete Event

I have an IE tool bar application under the DocumentComplete Event of the Page , i was disabling the HTML links using Concept of BHO(Browser Helper Object) that is working well for some of the sites but when i browse some AJAX/JQUERY based sites ,where the data will be popuplated dynamically and more over source code is also not vsible for dynamic data,this link disabling feature is not working.....
How To Disable or Modify the Content Of Dynamic Data When We load into browser??
HTMLDocument document = (HTMLDocument)webBrowser.Document;
IHTMLElementCollection hh = ((IHTMLElementCollection)document.getElementsByTagName("a"));
foreach (IHTMLElement ht in hh)
{
((HTMLAnchorElement)ht).removeAttribute("href", 1);
((HTMLAnchorElement)ht).style.color = "#b9b0b0";
}
Any Help ?
It depends on your IE version. Till IE 8 there is no DOM Mutation event generated but there is an alternative approach as mentioned here. Although IE 9 supports some basic dom mutation events as mentioned in this IE blog. Also, see this MSDN entry for the supported interface in IE9.
A very crude approach is to use a timer event for monitoring any changes in the DOM content. But you have to take care of threading issues and have to keep per window text information (by window I mean tabs and also IFrame content).
Another approach is to use Asynchronous Pluggable Protocols for monitoring any Ajax request and then accordingly notifying the appropriate tab/window. This is harder then the previous crude approach.
I got answer for the above mentioned problem. The following are the resolution steps what I have followed for the issue.
I just read the whole page content into "MSHTML" object as mentioned in the question
HTMLDocument document = (HTMLDocument)webBrowser.Document;
and again I just loaded it into a Web Browser control.
From there I can read the data easily and I just removed "href" property.

Html from Silverlight (not out of browser)

I am trying to open HTML file from the local URI which I use as XML Editor, to edit xml data that come from Silverlight application, then close browser window and return back edited xml data to the Silverlight application.
I tried to use HtmlPage.Window.Navigate but I don't quit like it.
I have tried using a method from: http://weblogs.asp.net/dwahlin/archive/2010/05/10/integrating-html-into-silverlight-applications.aspx
but instanly got an exception "failed to invoke ShowJobPlanIFrame"
Is there any way to handle this task?
"Out of browser" mode doesn't fit.
Thanks.
===========================================================================
Update:
It worked out using IFrame overlay.
Button click invokes the following code in C#:
var scriptObject = (ScriptObject)HtmlPage.Window.GetProperty("ShowJobPlanIFrame");
scriptObject.InvokeSelf(url);
Where "ShowJobPlanIFrame" is as defined at:
http://weblogs.asp.net/dwahlin/archive/2010/05/10/integrating-html-into-silverlight-applications.aspx
This allowed me to pass data into XML editor and then get it back.
An error with JavaScript function invocation I told above, was my fault in JavaScript code itself.
A very similar scenario: https://stackoverflow.com/a/7919065/384316
Try using an iframe overlay, then you can load any HTML-like content.
There is an excellent explanation of how to do this here:
http://www.wintellect.com/cs/blogs/jlikness/archive/2010/09/19/hosting-html-in-silverlight-not-out-of-browser.aspx
It worked out using IFrame overlay.
Button click invokes the following code in C#:
var scriptObject = (ScriptObject)HtmlPage.Window.GetProperty("ShowJobPlanIFrame");
scriptObject.InvokeSelf(url);
Where "ShowJobPlanIFrame" is as defined at:
http://weblogs.asp.net/dwahlin/archive/2010/05/10/integrating-html-into-silverlight-applications.aspx
This allowed me to pass data into XML editor and then get it back.
An error with JavaScript function invocation I told above, was my fault in JavaScript code itself.
Did you try NavigationFramework of Silverlight? It's capability may support your needs in a more simple way than using multiple browser pages.

How to submit data from an html page forcing the browser keeps the page unchanged

First let me set the situation.
I am writing a simple client html page and a simple server side program.
In detail, the page has a submit button to POST some data to the server program.
The problem is that any time I test the page to push the submit button ,the browser displays the new page which displays only the return message my server program returned.
How can I modify the html or the server side program so that the browser keeps the page unchanged before after the submit button is pushed.
I know an easiest way ; letting the sever program returns the same string as the client html page.
Thank you in advance.
In a regular form submission, your page will be whatever the server sends back. The form's action might be the same page, and then your server-side code can read the value of any input fields, and set the values in the response back to what they were in the request. This should result in the page looking the same as it did before the submit button was pressed. However, the page has "changed" in the sense that it was reloaded.
Alternatively, your form can make an Ajax request, which means that you'd need to use Javascript to intercept and stop the form submission, and then use additional coding to send the Ajax request, and then receive and process the response.
What you want is probably a postback:
http://en.wikipedia.org/wiki/Postback
1.) AJAX
You could use JavaScript and AJAX to POST the data.
2.) IFrame (not recommended)
You could also create a hidden IFrame and set the target attribute of the form to it.
What you want to do doesn't seem to be very clear.
If you want to submit your POST data without loading a new web page, you can use Ajax. It may be simple to do it in jQuery, and even simpler if you serialize your form data.
$('form').submit(function() {
$.post('your-post-url',$(this).serialize(),function(data) {
alert('Data posted!');
});
return false;
});