ARZ
ARZ

Reputation: 2491

GetElementsByTagName returns no elements in WebBrowser Control

This is my code:

    webBrowser1.ObjectForScripting = this;

    string str =
         "<html><head><script  type=\"text/javascript\">" +
         "var list = document.getElementsByTagName('abbr');" +
         "len = list.length;" +
         "for(i = 0;i < len;i++)" +
         "{obj=list[i];obj.onclick=window.external.Test(this.id);}" +
         "</script></head>" +
         "<body>";

    for (int i = 1000; i < 1100; i++)
    {
        str += "<abbr id=\'" + i.ToString() + "\'" +
        ">" + i.ToString() + " </abbr>";
    }
    str += "</body></html>";

    webBrowser1.DocumentText = str;

Thanks

Upvotes: 0

Views: 985

Answers (1)

anroesti
anroesti

Reputation: 11393

As you placed your script in the <head>, it gets executed before the contents of the <body> are fully loaded. There are two possibilities to avoid that problem: You could place the script before the ending </body>-Tag or you execute your script onload.

window.onload = function () {
    // Insert code that depends on a loaded body here.
}

Upvotes: 2

Related Questions