XHML 中的 Google 自定义搜索(或替代方案)
Google 的 JavaScript API 使用 document.write
函数,因此在 XHTML 中不可用。
您知道如何让自定义搜索在 XHTML 中工作的解决方法吗?或者有可行的替代方案吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您实际上是否将 XHTML 作为 XML (
application/xhtml+xml
) 提供?如果没有,您还不必担心。document.write
仍然可以在text/html
模式下工作,尽管这通常是不好的做法。如果您确实提供本机 XHTML...那么,我怀疑您可能会遇到的问题不仅仅是
document.write
,因为有相当多的事情可能会在脚本意外出现时出错。在 XHTML 中运行。但是您可以通过破坏document.write
来解决这个问题。最简单的方法是这样的:
但是,如果它尝试编写
标签,您将需要更多的混乱(因为通过
innerHTML
设置它们不会执行它们;您必须使用getElementsByTagName
挑选出它们并手动运行每个元素),或者在不同的write
调用中选取部分元素(在这种情况下,您必须完成后收集绳子并将它们粘在一起)。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 intext/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 sabotagingdocument.write
.The simplest method would be something like:
however you would need more messing around if it tried to write
<script>
tags (since setting them throughinnerHTML
doesn't execute them; you would have to pick them out withgetElementsByTagName
and run each one manually), or partial bits of elements across different calls towrite
(in which case you'd have to collect strings and glue them together when it's finished).