The Weird Parsing Rules of HTML `<script>` Tags and Escaping JSON
2025-08-09
This article delves into the often-unintuitive parsing rules of HTML's `` tags, which can surprisingly break web pages. It explains how browsers enter different states (script data, script data escaped, script data double escaped) while parsing content within `<script>` tags. Entering the 'script data double escaped' state prevents the closing `` tag from working correctly, leading to broken page rendering. The article recommends using `json_encode($data, JSON_HEX_TAG | JSON_UNESCAPED_SLASHES)` (or similar) to escape `<` and `>` characters in JSON strings, thereby avoiding the 'script data double escaped' state and ensuring correct page rendering.
(sirre.al)
Development