#67237·QGIS

HTML Widget: document.write() only works with expression.evaluate() in QGIS 4.2.1

Author: hrs-nodaCreated Sep 1, 2026Updated Oct 1, 2026
LabelsRegressionBugQGIS 4

What is the bug or the crash?

In QGIS 4.2.1, JavaScript document.write() does not appear to work normally in the HTML Widget.

The following specific form works:

However, ordinary document.write() does not work, and adding another JavaScript statement or another expression.evaluate() causes the output not to be displayed.

In particular:

  • document.write(expression.evaluate(...)) with one expression works.
  • Adding "var x=123;" causes it to stop working.
  • Adding a second document.write(expression.evaluate(...)) causes both outputs to disappear.
  • document.write("タイトル") does not display anything.

This appears to be a regression or unintended behavior in the HTML Widget.

Steps to reproduce the issue

Create an HTML Widget in an attribute form and test the following cases separately.

Test 1 — Works

Result: "タイトル" is displayed.

Test 2 — Does not work

Result: Nothing is displayed.

The only difference from Test 1 is the addition of "var x=123;".

Test 3 — Does not work

Result: Neither "タイトル" nor "タイトル2" is displayed.

Test 4 — Does not work

Result: Nothing is displayed.

These tests show that the problem is not limited to multiple expression.evaluate() calls. Even ordinary JavaScript document.write() does not work.

Versions

QGIS version: 4.2.1 Operating system: Windows

Supported QGIS version

  • I'm running a supported QGIS version according to the roadmap.

New profile

No Duplicate of the Issue

  • I have verified that there are no existing issues raised related to my problem.

Additional context

I investigated this behavior interactively with ChatGPT and used several minimal test cases to isolate the problem.

I also examined the QGIS 4.2.1 source code and found that QgsHtmlWidgetWrapper specifically searches the HTML for a pattern corresponding to:

using a regular expression.

The relevant implementation appears to use a pattern similar to:

The implementation then evaluates the captured QGIS expression on the QGIS side and replaces the matching HTML with the resulting value.

This appears to explain why the specific form:

works, while:

does not.

The regular expression also uses a greedy "(.*)" pattern, which may be relevant when multiple expression.evaluate() blocks are present.

The behavior also appears to be related to changes in the HTML Widget implementation between earlier QGIS versions and QGIS 4.2. In earlier versions, expression.evaluate() appears to have been exposed to the JavaScript environment through an HtmlExpression object, whereas the QGIS 4.2 implementation appears to detect and evaluate a specific document.write(expression.evaluate(...)) pattern before rendering.

I am not sure whether this was an intentional API/behavior change or an unintended regression.

Could the developers please clarify:

  1. Is ordinary JavaScript document.write() intentionally unsupported in the QGIS 4.2.1 HTML Widget?
  2. Is the special handling of document.write(expression.evaluate(...)) intentional?
  3. Should multiple expression.evaluate() blocks be supported?
  4. Should additional JavaScript statements be allowed inside the same