views:

364

answers:

4

The MSDN documentation on WebBrowser Customization explains how to prevent new windows from being opened and how to cancel navigation. In my case, my application is hosting an IWebBrowser2 but I don't want the user to navigate to new pages within my app. Instead, I'd like to open all links in a new IE window. The desired behavior is: user clicks a link, and a new window opens with that URL.

A similar question was asked and answered here and rather than pollute that answered post, it was suggested I open a new discussion.

The members on the related post suggested I should be able to do this by trapping DISPID_BEFORENAVIGATE2, setting the cancel flag, and writing code to open a new window, but I've found out that the browser control gets lots of BeforeNavigate2 events that seem to be initiated by scripts on the main page. For example, amazon.com fires BeforeNavigate2 events like crazy, and they are not a result of link invocation.

Replies appreciated!

A: 

It seems to me, that it you want "to open all links in a new IE window", it means that you want that the opening of new windows must be done in another process. The easiest way to do so: using CreateObject("InternetExplorer.Application") way (see another question which solve a problem, which is opposite to your question: http://stackoverflow.com/questions/503041/internetexplorer-application-object-and-cookie-container/2862352#2862352). With this way you will receive the best isolation from your application and the user who clicks on the link receive all possibilities which exist in IE. You should of cause continue usage of BeforeNavigate2 events to find out the moment when "a new IE window" should be opened.

Oleg
Oleg, you wrote: "continue usage of BeforeNavigate2 events to find out the moment when "a new IE window" should be opened" which is exactly the question I'm asking. AFAIK there is no way to differentiate between script-initiated BeforeNavigate2 events, and user-initiated BeforeNavigate2 events. Only the user-initiated events should result in a new window. (Whether that window can or should be opened in a new process or not, is not my question).
Rob McAfee
OK, Thanks! Now I understand. I should think about it. You are searching for some way of hooking some script functions? Could you describe a practical situation when you need such behavior? You can have close problems with using ActiveX components also.
Oleg
Oleg, I simply want links to open in new windows. The practical situation is that I'm writing an app that requires this behavior. I'm open to any solution. Trapping BeforeNavigate2 has been recommended many times, but I've found it to be insufficient since you can't tell which BeforeNavigate2 events were triggered by scripts and which were triggered by the user.
Rob McAfee
You can do control a lot of things by BeforeNavigate2. First of all you control the start page of browsing. Then you receive full html code of any page. You can scan all links, download and analyse the content. You can modify any page dynamically before displaying it to user. If you want full control you can realize it. I see no problem to force opening a link in a new windows, but denying some scripts is much more job. It can be realized, but I really not understand in what situation it is really needed. If it is confidential information, then no problem. I am curious. But why downgrading?
Oleg
Oleg, this doesn't answer the question as posted.
Georg Fritzsche
+1  A: 

You could try a different approach instead and physically add the attribute target="_blank" to all <a> tags in the rendered document.

This approach would involve waiting for DISPID_DOCUMENTCOMPLETE and then using IHTMLDocument3::getElementsByTagName() to fetch all of the anchor elements. You would then use IHTMLElement::setAttribute() to set target="_blank" on each of them.

Phil Booth
Phil, I'll give this a try. I tried something along these lines before posting the question (I set the <base target="_blank"> attribute in the <head> element) but I ran into problems: 1, the user can click before DocumentComplete arrives, and 2, I was getting an HRESULT that indicated write failure. The write failure may have been a symptom of something I was doing wrong, but the fact that the user could click links before the document was complete confounded things. I'll post more after digging into this again...
Rob McAfee
Update on this idea: setting the <target> attribute of all the <a> elements on the page works for those elements, but I still have the basic problem of scripts that initiate navigation. Amazon is a good example of a site that seems to use a lot of scripts to navigate rather than <a> elements.
Rob McAfee
Something to keep in mind - what happens with sites where scripts dynamically update the DOM?
Georg Fritzsche
Georg, you're right, any approach that alters the DOM is going to have problems with scripts. Hmmm...
Rob McAfee
+1  A: 

I'm hypothesising here but yet another approach could be to maintain a count of navigation events, incrementing the counter on DISPID_BEFORENAVIGATE2 and decrementing it on occurrences of DISPID_NAVIGATECOMPLETE2 and DISPID_NAVIGATEERROR. With that in place, you could speculate that whenever you get DISPID_BEFORENAVIGATE2 and your counter is at zero, it is actual user navigation / link invocation.

I have no idea whether this approach would work, or whether those are the right events you'd need to make it work, but it could be worth investigating.

Phil Booth
+2  A: 

Sorry to answer my own question here, but in the hopes that this may help somebody else, what I ended up doing was using IHTMLDocument directly rather than IWebBrowser. IWebBrowser is a superset of IHTMLDocument, and the navigation model implemented by IWebBrowser isn't customizable to the degree I wanted.

I actually got MS Developer Support involved and this approach was their recommendation. They say this is what Outlook uses for HTML-based email, which is the user experience I wanted to emulate. They also confirmed that there's no reliable way to filter the OnBeforeNavigate events that result from user action from those that result from script activity.

Hope this helps anybody facing the same issues. It wasn't too hard to port the code to use IHTMLDocument. If you end up doing this, you may also find yourself looking for a way to figure out when the document is done loading. To do that, hook HTMLDocumentEvents instead of DWebBrowserEvents, and look for the DISPID_HTMLDOCUMENTEVENTS_ONREADYSTATECHANGE event. It doesn't tell you what the ready state is; you need to call IHTMLDocument::get_readyState and parse the resulting string. Goofy, but there you go.

Rob McAfee
Wow, good work. It's nice to find out there is a definitive answer to this one.
Phil Booth