tags:

views:

671

answers:

2

I need to extract the javascript call in the onlick event defined in the following markup:

<div style="cursor: pointer;" onclick='javascript:start("a", "b", "code");'>Click Here</div></div>

This is what I want to extract from onclick as a text string: 'javascript:start("a", "b", "code");'

I am a novice at using MSHTML and this is what I tried so far and I am getting nowhere. Maybe there is a better way to do this?

foreach (mshtml.IHTMLElement elm in (IHTMLElementCollection)doc.body.all)
{
    if (elm.getAttribute("onclick", 0) != null)
    {
        if (elm.getAttribute("onclick", 0).ToString().Contains("javascript:start"))
        {
            Debug.WriteLine("Found!");
        }
    }    
}
A: 

I figured it out. Just check the outerHTML of the element. elm.outerHTML.ToLower().Contains("javascript:start")

John Sheares
A: 

The onclick attribute should not contain "javascript:" at all. It doesn't accept a javascript: URL as its value. It should just contain javascript expressions. (Although, if you do include "javascript:", then it is just treated by the scripting engine as a label with no adverse effects).

i.e. <p onclick="alert('hello');">This is correct</p>

<p onclick="javascript:alert('hello');">This is not ideal.</p>

So then assuming the markup being parsed does this correctly, then your code above should not check for the inclusion of the string "javascript:". It should just be:

if (elm.getAttribute("onclick", 0).ToString().Contains("start"))

Lachlan Hunt