Monday, February 13, 2012

Daily Javascript: input arguments to scripts

LinkedIn integration instructions provide the following syntax:
<script type="text/javascript" src="http://platform.linkedin.com/in.js">
api_key: your_api_key_goes_here
authentication: true
</script>

we say, WTF? what kind of script tag has a src attribute and content as well?

It turns out that the JS in the src attribute is pulling those lines out of the DOM and parsing them. It is not really JS inside there, but a language specific to in.js. Wow, that's cryptic and unexpected.

What is a better way to pass arguments into scripts like this one?

Our method at work, so far, is

  1. Inside the included script, do nothing except define a giant function.

  2. Inside another script tag (this one with no src attribute), call that giant function, passing in whatever arguments it needs.


This allows us to pass in values that may be filled in by, say, our velocity template.
Anybody have a cleaner pattern?

No comments:

Post a Comment