Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

google +1 javascript configuration object

The google +1 button embed code can have a javascript object with configuration (e.g. "{lang:'de'}").

In plain javascript, this object would be created and immediately destroyed, because it is not referenced by anything.

I wonder how do the google scripts access this object?

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

It seems to work - except when you dynamically write the script tag including the configuration object into the DOM.

like image 378
Frunsi Avatar asked Jun 29 '11 15:06

Frunsi


1 Answers

As the google source is obfuscated I don't know how they do it. How I would do it is use JQuery to find the tag and then use .innerHTML to get this as a string then use JSON.parse to parse the object safely.

<script ...>
 {"lang" : "de"}
</script>
...
var data= JSON.parse(
    $('script[src="https://apis.google.com/js/plusone.js"]')[0].innerHTML)
alert(data.lang)
like image 151
David Waters Avatar answered Oct 22 '22 04:10

David Waters