Google的+1按钮:他们是如何做到的? [英] Google's +1 Button: How do they do it?
问题描述
浏览Google的+1按钮后,我发现它们提供的代码有两点奇怪:
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>
所以我有两个问题:
第一: Google如何使用 script
标记之间的文本?
第二个: 语法< g:plusone ...
HTML是否有效?这叫什么?
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?
推荐答案
Google如何使用脚本标记之间的文本?
How is Google able to use the text between the script tags?
< script>
元素在DOM中完全可见:
<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的偷偷摸摸的技巧是将内容放入具有外部 src
的< script>
中.在这种情况下, src
会覆盖块内的内容并执行外部脚本,但是即使通过DOM仍可以读取< 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.
语法
< g:plusone
... HTML是否有效?这叫什么?
Is the syntax
<g:plusone
... HTML valid? What's this called?
不.如果他们为HTML + plusone编写了自己的文档类型,那么它可能是有效的 that ,但是它不能满足HTML的有效性,并且在XHTML文档中它甚至都不是命名空间格式良好的格式,除非您也为此添加了一个额外的 xmlns:g
.
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.
这篇关于Google的+1按钮:他们是如何做到的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!