An embeddable JSONP javascript widget template

Recently I’ve been hacking together a little online poll creator - pollfu. For the app I needed to have the option to embed the poll on an external website. One of the best examples of this is the checkout widget by stripe. The cool thing about the stripe widget is it loads everything it needs through that one script.

This is the core of what I came up with:

This is what it can do:

After you have jquery and your scripts loaded in you can use it grab data from your server using jsonp (there is a super simple example commented out in the source).

Another cool thing to try is setting widget variables on the script tag using data attirbutes (eg data-id=”1”). Once you have a reference to the script tag these are super easy to read in eg:

<script src="http://pollfu.com/public/js/embed.v1.js" data-id="9"></script>

This is an example of the end result embedded poll for pollfu: