SHDocVw - WebBrowserClass- NewWindow2 not fire

  • Thread starter Thread starter Lauren Hines
  • Start date Start date
L

Lauren Hines

I have created a c# .dll that is registered as a 'band object' for IE. It's
purpose is to count pop-up windows, which is accomplished in part using the
NewWindow2 event that I am catching.

My problem occurs when I come across a web page with the following java
script:

function leave() {
window.open('testclose.htm','','');
}
........
onunload="leave()"


Is there anyway I can catch this event, and distinguish it from someone just
launching an instance if IE?

Thank you,
Lauren Hines
 
Lauren,

What you could do is work through the document object model. Basically,
the DOM will have a reference to an object for the "leave" function that is
the event handler. What you can do in the BHO is wait until the DOM has
been constructed (there is an event that fires). Once that happens, get the
value of the onunload property. This will be an object which implements the
IDispatch interface. The implementation will call the "leave" method for
the method that has a DISPID of 0, and no parameters.

When the page is loaded, you want to replace the value of the onunload
property with your own object, which has a method with no parameters, and no
return value. Place the DispId attribute on this method. Then, store the
old value of the onunload property in your class. Once you have this,
replace the value with a reference to your class. COM interop will handle
all the hookup. When your method is called, you can do your processing, and
then call the old method by using reflection, getting the method where the
DISPID is zero, and then executing that.

Hope this helps.
 
I am able to obtain access to the 'onunload' object in my
OnDocumentCompleteEventHandler as follows:

mshtml.HTMLWindow2 parentWindow = (mshtml.HTMLWindow2)doc.parentWindow;


if (parentWindow.onunload != null)

......



But I'm not sure what to do after that - I can't figure out how to 'set' the
onunload. Am I going about this the wrong way?

Thanks again.

Nicholas Paldino said:
Lauren,

What you could do is work through the document object model. Basically,
the DOM will have a reference to an object for the "leave" function that is
the event handler. What you can do in the BHO is wait until the DOM has
been constructed (there is an event that fires). Once that happens, get the
value of the onunload property. This will be an object which implements the
IDispatch interface. The implementation will call the "leave" method for
the method that has a DISPID of 0, and no parameters.

When the page is loaded, you want to replace the value of the onunload
property with your own object, which has a method with no parameters, and no
return value. Place the DispId attribute on this method. Then, store the
old value of the onunload property in your class. Once you have this,
replace the value with a reference to your class. COM interop will handle
all the hookup. When your method is called, you can do your processing, and
then call the old method by using reflection, getting the method where the
DISPID is zero, and then executing that.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Lauren Hines said:
I have created a c# .dll that is registered as a 'band object' for IE. It's
purpose is to count pop-up windows, which is accomplished in part using the
NewWindow2 event that I am catching.

My problem occurs when I come across a web page with the following java
script:

function leave() {
window.open('testclose.htm','','');
}
.......
onunload="leave()"


Is there anyway I can catch this event, and distinguish it from someone just
launching an instance if IE?

Thank you,
Lauren Hines
 
I have also tried something like the following, but am not exactly sure how
to create 'MyUnload'.

object oattrib = doc.activeElement.getAttribute("onunload",0);
doc.activeElement.setAttribute("onunload",this.MyUnload(),0);


Nicholas Paldino said:
Lauren,

What you could do is work through the document object model. Basically,
the DOM will have a reference to an object for the "leave" function that is
the event handler. What you can do in the BHO is wait until the DOM has
been constructed (there is an event that fires). Once that happens, get the
value of the onunload property. This will be an object which implements the
IDispatch interface. The implementation will call the "leave" method for
the method that has a DISPID of 0, and no parameters.

When the page is loaded, you want to replace the value of the onunload
property with your own object, which has a method with no parameters, and no
return value. Place the DispId attribute on this method. Then, store the
old value of the onunload property in your class. Once you have this,
replace the value with a reference to your class. COM interop will handle
all the hookup. When your method is called, you can do your processing, and
then call the old method by using reflection, getting the method where the
DISPID is zero, and then executing that.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Lauren Hines said:
I have created a c# .dll that is registered as a 'band object' for IE. It's
purpose is to count pop-up windows, which is accomplished in part using the
NewWindow2 event that I am catching.

My problem occurs when I come across a web page with the following java
script:

function leave() {
window.open('testclose.htm','','');
}
.......
onunload="leave()"


Is there anyway I can catch this event, and distinguish it from someone just
launching an instance if IE?

Thank you,
Lauren Hines
 
Lauren,

Attached you will find an example of how to do this. There is a form
with a web control browser on it. When the document is loaded, the onclick
event handler is replaced with a method on the form which has a DISPID of 0.
What you have to do is store the old value (it's an object) and then have
the new method do what you want it to do and then call the old event handler
through reflection.

Hope this helps.

--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Lauren Hines said:
I am able to obtain access to the 'onunload' object in my
OnDocumentCompleteEventHandler as follows:

mshtml.HTMLWindow2 parentWindow = (mshtml.HTMLWindow2)doc.parentWindow;


if (parentWindow.onunload != null)

.....



But I'm not sure what to do after that - I can't figure out how to 'set' the
onunload. Am I going about this the wrong way?

Thanks again.

message news:[email protected]...
Lauren,

What you could do is work through the document object model. Basically,
the DOM will have a reference to an object for the "leave" function that is
the event handler. What you can do in the BHO is wait until the DOM has
been constructed (there is an event that fires). Once that happens, get the
value of the onunload property. This will be an object which implements the
IDispatch interface. The implementation will call the "leave" method for
the method that has a DISPID of 0, and no parameters.

When the page is loaded, you want to replace the value of the onunload
property with your own object, which has a method with no parameters,
and
no
return value. Place the DispId attribute on this method. Then, store the
old value of the onunload property in your class. Once you have this,
replace the value with a reference to your class. COM interop will handle
all the hookup. When your method is called, you can do your processing, and
then call the old method by using reflection, getting the method where the
DISPID is zero, and then executing that.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Lauren Hines said:
I have created a c# .dll that is registered as a 'band object' for IE. It's
purpose is to count pop-up windows, which is accomplished in part
using
the
NewWindow2 event that I am catching.

My problem occurs when I come across a web page with the following java
script:

function leave() {
window.open('testclose.htm','','');
}
.......
onunload="leave()"


Is there anyway I can catch this event, and distinguish it from
someone
just
launching an instance if IE?

Thank you,
Lauren Hines
 
Thanks so much - I ended up making a temporary hack which seemed to be
working, but I like your way much better. Thanks for your help!

Lauren

Nicholas Paldino said:
Lauren,

Attached you will find an example of how to do this. There is a form
with a web control browser on it. When the document is loaded, the onclick
event handler is replaced with a method on the form which has a DISPID of 0.
What you have to do is store the old value (it's an object) and then have
the new method do what you want it to do and then call the old event handler
through reflection.

Hope this helps.

--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Lauren Hines said:
I am able to obtain access to the 'onunload' object in my
OnDocumentCompleteEventHandler as follows:

mshtml.HTMLWindow2 parentWindow = (mshtml.HTMLWindow2)doc.parentWindow;


if (parentWindow.onunload != null)

.....



But I'm not sure what to do after that - I can't figure out how to 'set' the
onunload. Am I going about this the wrong way?

Thanks again.

message news:[email protected]...
Lauren,

What you could do is work through the document object model. Basically,
the DOM will have a reference to an object for the "leave" function
that
is
the event handler. What you can do in the BHO is wait until the DOM has
been constructed (there is an event that fires). Once that happens,
get
the
value of the onunload property. This will be an object which
implements
the
IDispatch interface. The implementation will call the "leave" method for
the method that has a DISPID of 0, and no parameters.

When the page is loaded, you want to replace the value of the onunload
property with your own object, which has a method with no parameters,
and
no
return value. Place the DispId attribute on this method. Then, store the
old value of the onunload property in your class. Once you have this,
replace the value with a reference to your class. COM interop will handle
all the hookup. When your method is called, you can do your
processing,
and
then call the old method by using reflection, getting the method where the
DISPID is zero, and then executing that.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

I have created a c# .dll that is registered as a 'band object' for IE.
It's
purpose is to count pop-up windows, which is accomplished in part using
the
NewWindow2 event that I am catching.

My problem occurs when I come across a web page with the following java
script:

function leave() {
window.open('testclose.htm','','');
}
.......
onunload="leave()"


Is there anyway I can catch this event, and distinguish it from someone
just
launching an instance if IE?

Thank you,
Lauren Hines
 
Back
Top