XHML 中的 Google 自定义搜索(或替代方案)

发布于 2024-08-29 20:25:28 字数 130 浏览 5 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

把梦留给海 2024-09-05 20:25:28

您实际上是否将 XHTML 作为 XML (application/xhtml+xml) 提供?如果没有,您还不必担心。 document.write 仍然可以在 text/html 模式下工作,尽管这通常是不好的做法。

如果您确实提供本机 XHTML...那么,我怀疑您可能会遇到的问题不仅仅是 document.write,因为有相当多的事情可能会在脚本意外出现时出错。在 XHTML 中运行。但是您可以通过破坏 document.write 来解决这个问题。

最简单的方法是这样的:

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

但是,如果它尝试编写

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).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文