Home > Backend Development > C++ > How to Correctly Inject JavaScript into an HTML Document to Avoid `NotSupportedException`?

How to Correctly Inject JavaScript into an HTML Document to Avoid `NotSupportedException`?

DDD
Release: 2025-01-26 21:21:09
Original
272 people have browsed it

How to Correctly Inject JavaScript into an HTML Document to Avoid `NotSupportedException`?

The method of injection JavaScript in the HTML document

By injecting the JavaScript code in the webpage, dynamic elements can be used to enhance the webpage function. However, the standard HTMLDOCUMENT method may encounter errors. This guide provides a solution to this problem.

The problem is located

The following code sample demonstrates the attempt to inject JavaScript into the document:

However, the InnerHTML attribute accessing Scriptel will throw the NotSupportedException exception. This shows that the HTMLEMENT type does not support the internal HTML for the script element.

<code>string newScript = textBox1.Text;
HtmlElement head = browserCtrl.Document.GetElementsByTagName("head")[0];
HtmlElement scriptEl = browserCtrl.Document.CreateElement("script");
lblStatus.Text = scriptEl.GetType().ToString();
scriptEl.SetAttribute("type", "text/javascript");
head.AppendChild(scriptEl);
scriptEl.InnerHtml = "function sayHello() { alert('hello') }";</code>
Copy after login
Solution

In order to solve this problem, we need to access the underlying IHTMLSCRIPTELEMENT interface. The following updated code fragment demonstrates this method:

Explanation

<code>HtmlElement head = webBrowser1.Document.GetElementsByTagName("head")[0];
HtmlElement scriptEl = webBrowser1.Document.CreateElement("script");
IHTMLScriptElement element = (IHTMLScriptElement)scriptEl.DomElement;
element.text = "function sayHello() { alert('hello') }";
head.AppendChild(scriptEl);
webBrowser1.Document.InvokeScript("sayHello");</code>
Copy after login

Get the head element of the document. Create a new script element.

    Retrieve the underlying IHTMLSCRIPTELEMENT interface from the domelement attribute of the script element.
  1. Set the Text property of the script element with the JavaScript code.
  2. Attach the script element to the head of the document.
  3. The JavaScript function is performed from the IHTMLSCRIPTELEMENT interface to activate the injected script.
  4. This method allows you to successfully inject JavaScript into the webpage and dynamically interact with its element.

The above is the detailed content of How to Correctly Inject JavaScript into an HTML Document to Avoid `NotSupportedException`?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template