I want to know the url of an image I triggered a context menu on(Internet Explorer)

104 views Asked by At

I followed the instructions from this source : http://www.codeguru.com/cpp/com-tech/atl/atl/article.php/c11007/Customize-an-IE-Context-Menu-to-Add-CodeGuru-Favorites.htm to add a context menu entry and to call a method of an ActiveX control through Javascript. This is the important part :

<SCRIPT LANGUAGE="JavaScript">
var parentwin = external.menuArguments; 
var doc = parentwin.document;
var str = new String(parentwin.event.srcElement.name);
var oFav = new ActiveXObject("CodeguruFavorites.CGFavorites");
oFav.ShowDefaultContextMenu(parentwin,doc.title, doc.location);
</SCRIPT>

In this example, the author sends to the ActiveX method the title of the web page and the URL of the web page I triggered the context menu on.

I want to know how to get the URL of the image I triggered the context menu on.

1

There are 1 answers

0
evilwhaleboy On

I found the answer in this post: How can one identify the currently clicked link with Javascript?.

The object on which the context menu is triggered is menuArguments.event.srcElement . It is passed to the ActiveX control's method as an IDispatch interface pointer. On this pointer you can call QueryInterface to get a pointer to a IHTMLImgElement.

pDispatch->QueryInterface(IID_IHTMLElement,(void**)&lpElement);
if (lpElement)
{
    CComBSTR tagName;

    lpElement->get_tagName(&tagName);

    if(0 == wcsicmp(tagName.m_str, L"img"))
    {
        CComPtr<IHTMLImgElement> spImg;

        lpElement->QueryInterface(IID_IHTMLImgElement, (void**)(&spImg));

        if(spImg)
        {
            CComBSTR src;

            HRESULT hr = spImg->get_src(&src);
            if(FAILED(hr))
                return hr;
        }
    }
}