Google Custom Search (or Alternative) in XHML

The Google JavaScript API uses a function document.write

, so it cannot be used in XHTML.

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

+2


a source to share


1 answer


Are you really using XHTML like XML ( application/xhtml+xml

)? If not, you don't have to worry about it. document.write

will still work in mode text/html

, although of course this is bad practice.

If you are indeed using native XHTML ... well, I suspect you might have more trouble than just document.write

, as there are quite a few things that can run scripts when they didn't expect them to run in XHTML. But you can hack this problem by sabotaging document.write

.

The simplest method would be something like:



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

      

however, you will need to mess around more if it tries to write tags <script>

(since setting them via innerHTML

does not execute them, you will have to select them with getElementsByTagName

and run each one manually), or partial bits of elements in different calls write

(in which case you will have to collect strings and glue them together when complete).

+2


a source







All Articles