PHP 8.5.0 Alpha 2 available for testing

Voting

: max(one, five)?
(Example: nine)

The Note You're Voting On

info at troptoek dot com
17 years ago
A common issue seems to be adding javascript to CDATA and the browser throwing a javascript error. To ensure the javascript works use the following code when adding CDATA:

<?php
/**
* Append Caracter Data to a node and check for a javascript node
*
* @param DOMElement $appendToNode
* @param string $text
*/
function appendCdata($appendToNode, $text)
{
if (
strtolower($appendToNode->nodeName) == 'script') { // Javascript hack
$cm = $appendToNode->ownerDocument->createTextNode("\n//");
$ct = $appendToNode->ownerDocument->createCDATASection("\n" . $text . "\n//");
$appendToNode->appendChild($cm);
$appendToNode->appendChild($ct);
} else {
// Normal CDATA node
$ct = $appendToNode->ownerDocument->createCDATASection($text);
$appendToNode->appendChild($ct);
}
}
?>
The result should be:

<script type="text/javascript">
//<![CDATA[
function someJsText() {
document.write('Some js with <a href="#">HTML</a> content');
}
//]]></script>

<< Back to user notes page

To Top