document.write and xhtml
Written by admin on November 11th, 2006 in Ajax News.
Sam Ruby knew that document.write doesn’t work well with XHTML so he went to find the best solution.
His context:
Because Google AdSense depends on document.write, the net result is that I only serve ads to users of browsers that don’t support XHTML, which increasingly means that only IE users see ads.
His solution:
The solution is to use createElementNS instead. So far, so good. The only piece left to the puzzle is where to append the child that you created. If you simply do a document.appendChild, the new element ends up at the end of the document. There doesn’t seem to be a property which indicates the current node in the tree at the time of the parse. But in cases like adsense, you generally want the widget put in place.
The code:
while (pos.lastChild.nodeType == 1) pos = pos.lastChild;
pos.parentNode.appendChild(…);
Could document.write be redefined if in an xhtml document to parse into a tree and append?
Source: Ajaxian
Original Article: http://ajaxian.com/archives/documentwrite-and-xhtml