DOM: Javascript vs. C++
Musste das Syntax-Highlighting wegmachen, das hat das Layout zerstört
Ich geh gerade mal sterben. Beide Funktionen haben dieselbe Funktionalität, nämlich alles Bilder innerhalb einer Webseite zu “verstecken”. Hier der Javascript-Code
function RemoveImages(doc) {
var images = doc.images;
if (images != null) {
for (var i = 0; i < images.length; i++) {
var img = images.item(i);
img.style.display = "none";
}
}
}
Hurra. Nun in C++
void CHelloWorldBHO::RemoveImages(IHTMLDocument2* pDocument) {
CComPtr<IHTMLElementCollection> spImages;
// Get the collection of images from the DOM.
HRESULT hr = pDocument->get_images(&spImages;);
if (hr == S_OK && spImages != NULL) {
// Get the number of images in the collection.
long cImages = 0;
hr = spImages->get_length(&cImages);
if (hr == S_OK && cImages > 0) {
for (int i = 0; i < cImages; i++) {
CComVariant svarItemIndex(i);
CComVariant svarEmpty;
CComPtr<IDispatch> spdispImage;
// Get the image out of the collection by index.
hr = spImages->item(svarItemIndex, svarEmpty, &spdispImage);
if (hr == S_OK && spdispImage != NULL) {
// First, query for the generic HTML element interface...
CComQIPtr<IHTMLElement> spElement = spdispImage;
if (spElement) {
// ...then ask for the style interface.
CComPtr<IHTMLStyle> spStyle;
hr = spElement->get_style(&spStyle;);
// Set display="none" to hide the image.
if (hr == S_OK && spStyle != NULL) {
static const CComBSTR sbstrNone(L"none");
spStyle->put_display(sbstrNone);
}
}
}
}
}
}
}
Kommentar:
Interacting with the DOM in C++ is more verbose than JavaScript, but the code flow is essentially the same.
Quelle: http://msdn.microsoft.com/en-us/library/bb250489.aspx
- Registry Pattern in Javascript
- Vererbung in Javascript anhand von Exceptions
- Wie Weihnachten
- Java Collection Interface
- Javascript-Objekte
If you enjoyed this post, please consider to leave a comment or subscribe to the feed and get future articles delivered to your feed reader.

Jaja, immer die Verhältnismäßigkeit beachten. Versuch mal ein Betriebssystem mit JavaScript zu programmieren.
Mit einem Hammer sägt es sich eben schlecht.
guckst du hier
Ok, ist kein OS … ich weiß ja was du meinst