document.write
does not work but setting innerHTML
is still working. Just remember that whatever you set in there must be well-formed.
This means that you can use jQuery to add new element/set html to any element.
I have a small library used for this. It work quite a bit.
function IsXHTML() {
if (document.doctype == null)
return false;
var vPublic = document.doctype.publicId.replace(/^.*(.html).*$/i, '$1').toLowerCase();
var vSystem = document.doctype.systemId.replace(/^.*(.html).*$/i, '$1').toLowerCase();
return (vPublic == 'xhtml')
&& (vSystem == 'xhtml');
};
function XHTMLDocWrite($Str, $Element) {
if ($Str == null)
return;
var vIsMozilla = !window.opera && !/Apple/.test(navigator.vendor);
var vIsOpera = window.opera;
// Make sure &s are formatted properly
if (!vIsOpera)
$Str = $Str.replace(/&(?![#a-z0-9]+;)/g, "&");
else $Str = $Str.replace(/&(?![#a-z0-9]+;)/g, "&");
// - Mozilla assumes that everything in XHTML, innerHTML is actually XHTML
// - Opera and Safari assume that it's XML
if ( !vIsMozilla )
$Str = $Str.replace(/(<[a-z]+)/g, "$1 xmlns='http://www.w3.org/1999/xhtml'");
// The HTML needs to be within a XHTML element
var vDiv = document.createElementNS("http://www.w3.org/1999/xhtml","div");
vDiv.innerHTML = $Str;
if ($Element == null) {
// Find the last element in the document
var vLastElmt = document.getElementsByTagName("*");
vLastElmt = vLastElmt[vLastElmt.length - 1];
$Element = vLastElmt;
}
// Add all the nodes in that position
var vNodes = vDiv.childNodes;
while (vNodes.length)
$Element.parentNode.appendChild( vNodes[0] );
};
function UseXHTMLDocWrite($IsForced) {
if (!IsXHTML() && !$IsForced)
return;
if (document.write == XHTMLDocWrite)
return;
document.write = XHTMLDocWrite;
};
You run UseXHTMLDocWrite
so that document.write
will be replaced with XHTMLDocWrite
if needed.
This will simply add content to the last added element. However, it does not work with nested element.
NOTE: I modify the code of XHTMLDocWrite
from the code I got from some where on the Internet and cannot seems to find it anymore. So sorry I can't credit him.
Hope this helps.