打电话时document.GetElementsByTagName奇怪的错误(; HEAD"&QUOT) [英] Weird error when calling document.GetElementsByTagName("head")

查看:208
本文介绍了打电话时document.GetElementsByTagName奇怪的错误(; HEAD"&QUOT)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用以下code在Web页面中注入的JavaScript应用程序中的 web浏览器

I have an application that uses the following code to inject JavaScript in a web page in a WebBrowser:

HtmlElement head = document.GetElementsByTagName("head")[0];
HtmlElement scriptEl = document.CreateElement("script"); 
IHTMLScriptElement element = (IHTMLScriptElement)scriptEl.DomElement; 
element.text = CurrentFuncs; 
head.AppendChild(scriptEl); 

不过,我刚刚从那个有一个异常客户的错误报告的 document.GetElementsByTagName(头)[0] 一张code表示说:0的数值是无效的索引索引应介于0和-1。我是pretty的肯定这是从 [0] 的事情在该行的code,但不知道为什么。

But I just got an error report from a customer that got an exception in the document.GetElementsByTagName("head")[0] piece of code that says: "Value of '0' is not valid for 'index'. 'index' should be between 0 and -1." I'm pretty sure that's from the [0] thing in that line of code, but no idea why.

我想是因为没有头的元素。我刚刚上传了一个页面,没有头,与我的应用程序中打开它,但错误没有重现。该 web浏览器自动添加head元素。我甚至尝试上传一个名为.txt文件,仍然没有错误。知道为什么会这样发生或我怎么能重现错误?

I assume is because there is no "head" element. I just uploaded a page with no head and opened it with my application, but the error didn't reproduce. The WebBrowser automatically adds the "head" element. I even tried uploading a ".txt" file and still no error. Any idea why could this be happening or how could I reproduce the error?

不幸的是我不知道就发生了网页中的错误做。

Unfortunately I don't know on which web page the error occurred.

推荐答案

我怀疑你的code是这样的:

I suspect your code looks like this:

string url = "http://www.google.com";
webBrowser1.Navigate(url);
HtmlDocument document = webBrowser1.Document;
HtmlElement head = document.GetElementsByTagName("head")[0];
HtmlElement scriptEl = document.CreateElement("script");
mshtml.IHTMLScriptElement element = (mshtml.IHTMLScriptElement)scriptEl.DomElement;
element.text = "alert('1');";
head.AppendChild(scriptEl);

现在的问题是,经过导航直,该文件是没有加载。你将需要移动访问该文件到DocumentCompleted处理程序中的code中的一部分。

The problem is that straight after Navigate, the document isn't loaded yet. You will need to move the part of the code that access the document into the DocumentCompleted handler.

private void Go()
{
    webBrowser1.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(webBrowser1_DocumentCompleted);
    string url = "http://www.google.com";
    webBrowser1.Navigate(url);
}
void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
    HtmlDocument document = webBrowser1.Document;
    HtmlElement head = document.GetElementsByTagName("head")[0];
    HtmlElement scriptEl = document.CreateElement("script");
    mshtml.IHTMLScriptElement element = (mshtml.IHTMLScriptElement)scriptEl.DomElement;
    element.text = "alert('1');";
    head.AppendChild(scriptEl);
    // Code here
}

这篇关于打电话时document.GetElementsByTagName奇怪的错误(; HEAD"&QUOT)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆