views:

22

answers:

1

Google's JavaScript API makes use of the function document.write, thus is not usable in XHTML.

Do you know a workaround how to get the custom search working in XHTML? Or is there a working alternative?

+2  A: 

Are you actually serving your XHTML as XML (application/xhtml+xml)? If not, you don't have to worry about it, yet. document.write will still work in text/html mode though it is certainly poor practice in general.

If you really are serving native XHTML... well, I suspect you may get more problems than just document.write, as there are a fair few things that can trip scripting up when it's not expecting to be run in XHTML. But you can hack it the problem by sabotaging document.write.

The simplest method would be something like:

document.write= function(s) {
   document.getElementById('placetoputwrittenstuff').innerHTML= s;
};

however you would need more messing around if it tried to write <script> tags (since setting them through innerHTML doesn't execute them; you would have to pick them out with getElementsByTagName and run each one manually), or partial bits of elements across different calls to write (in which case you'd have to collect strings and glue them together when it's finished).

bobince