Google 的 +1 按钮:他们是如何做到的?

发布于 2024-11-24 09:38:46 字数 485 浏览 3 评论 0 原文

在探索 Google 的 +1 按钮时,我发现他们提供的代码有两点奇怪:

<script type="text/javascript" src="https://apis.google.com/js/plusone.js">
  {lang: 'en-GB'}
</script>

<g:plusone size="tall" href="http://www.google.com"></g:plusone>

所以我有两个问题:
第一:       Google 如何使用 script 标记之间的文本?
第二:  语法 HTML 有效吗?这叫什么?

Exploring Google's +1 Button, I found two things odd about the code they supply:

<script type="text/javascript" src="https://apis.google.com/js/plusone.js">
  {lang: 'en-GB'}
</script>

<g:plusone size="tall" href="http://www.google.com"></g:plusone>

So I have two questions:
First:       How is Google able to use the text between the script tags?
Second:  Is the syntax <g:plusone ... HTML valid? What's this called?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

梦年海沫深 2024-12-01 09:38:46

Google 如何使用脚本标记之间的文本?

<script type="text/javascript">//FIRST SCRIPT BLOCK</script>

<script type="text/javascript">
    var s= document.getElementsByTagName('script')[0];
    alert(s.textContent); // "//FIRST SCRIPT BLOCK"
</script>

Google 的狡猾伎俩是将内容放入具有外部 src

语法 ... HTML 有效吗?这叫什么?


不。如果他们为 HTML+plusone 创建了自己的文档类型,那么它可能是有效的,但它不满足 HTML 的有效性,而且在 XHTML 文档中它甚至不是命名空间格式良好的,除非您也为其添加额外的 xmlns:g

How is Google able to use the text between the script tags?

<script> elements are perfectly visible in the DOM:

<script type="text/javascript">//FIRST SCRIPT BLOCK</script>

<script type="text/javascript">
    var s= document.getElementsByTagName('script')[0];
    alert(s.textContent); // "//FIRST SCRIPT BLOCK"
</script>

Google's sneaky trick is to put content in a <script> that has an external src. In this case the src overrides the content inside the block and executes the external script instead, but the contents of the <script> element are still readable through the DOM even though they do nothing.

Is the syntax <g:plusone ... HTML valid? What's this called?

No. If they made their own doctype for HTML+plusone it could be valid that, but it doesn't satisfy validity for HTML, and it isn't even namespace-well-formed in an XHTML document, unless you add an extra xmlns:g for it too.

窗影残 2024-12-01 09:38:46

语法 ... HTML 有效吗?


这叫什么?

无效的伪命名空间

Is the syntax <g:plusone ... HTML valid?

No

What's this called?

Invalid psuedo-namespaces

甜宝宝 2024-12-01 09:38:46

第一个技巧很有趣。它看起来像是一种将“全局”参数从页面标记传递到外部脚本的创造性方法。有方法来查找获取当前正在运行的代码的

在您的问题中,此模式允许每个外部客户端脚本(至少)使用自己的本地化设置,并且还允许服务器端代码渲染该参数作为渲染

第二个技巧,我不太确定。基本上,我认为大多数浏览器都会将命名空间 元素视为未知甚至无效,因此它们应该 渲染其内容,但当然它不会执行任何操作,因为该元素一开始就是空的。

但是,客户端代码可能仍然能够使用 DOM 导航来匹配命名空间元素,并将其替换为自己生成的内容。

The first trick is interesting. It looks like a creative way to pass "global" arguments from the page markup to external scripts. There are ways to find the <script> element that sources the code that's currently running, and I would not be surprised if the inner text of that <script> element was accessible from the DOM even though the browser ignores it.

In your question, this pattern allows each external client-side script to use (at least) its own localization settings, and also allows server-side code to render that parameter as a side effect of rendering the <script> element itself. That's impressive.

The second trick, I'm not so sure about. Basically, I think most browsers would consider the namespaced <g:plusone> element as unknown or even invalid, so they should render its content, but it won't do anything, of course, since that element is empty to begin with.

However, client-side code might still be able to match the namespaced element using DOM navigation, and replace it with its own generated content.

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