Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google's +1 Button: How do they do it?

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?

like image 515
Shaz Avatar asked Jul 15 '11 21:07

Shaz


People also ask

What does Google's plus 1 mean to you now?

As Google puts it: “The 1+ button is shorthand for 'this is pretty cool' or 'you should check this out. ' Click +1 to publicly give something your stamp of approval.

What is a Google button?

A personalized button displays profile information for the first Google session that approves your website.


2 Answers

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.

like image 105
bobince Avatar answered Oct 13 '22 23:10

bobince


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

No

What's this called?

Invalid psuedo-namespaces

like image 35
Quentin Avatar answered Oct 13 '22 23:10

Quentin