1

Sometimes you see code like this.

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

I'd like to know how it's possible to parse the object literal inside this script tag from the loaded script.

2

1 Answer 1

2
var scripts = document.getElementsByTagName('script');
var thisScriptTag = scripts[scripts.length - 1];
var data = thisScriptTag.textContent || thisScriptTag.innerText;
alert(data);

If you have JSON data you'd use JSON.parse() to convert the data to a JavaScript object. Note that the code must not be wrapped in a DOMready/onload event - it needs to run right when that <script> tag is processed.

The code to get the current script tag was taken from How may I reference the script tag that loaded the currently-executing script?

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.