I'm deciding on how to generate the code to allow users generate an embeddable widget (much like the StackOverflow badge) into external websites.

The content of the embedded widget will periodically change, but it doesn't need to be communicating in real-time with my server after a visitor has loaded the page where it's embedded.

This opens up several possibilities for getting this to work:

  1. On the server, pre-generate the static html content I want rendered in their page (and have a scheduled job which regenerates the static file). My users can then embed it in their page using an iframe reference to the static resource.

  2. Same as above, but instead of the iframe I create a js file which they reference (much like an Google Analytics code) and then the js file served inserts the data in into their DOM. My web server would need to dynamically generate the js file on each request for the file resource.

  3. Give them a js file which creates the element on their page like the above, except the js file payload doesn't include all the data and instead basically generates the DOM element as a template, then calls web services to populate the data (like their name and score) using JSONP requests to my server.

I like 2 for the explicit server-side control, but it will take more time. 3 is good because the services I expose to retrieve this data can be reused for other purposes later. I don't generally like iframes, but they work and are very quick to implement.

Any suggestions of which way to proceed or ideas that I've missed?

1 Answer 1


I'd go with #3. #2 and #3 have basically the same difficulty (DOM manipulation in Javascript and catering to, or at least investigating, the quirks of different browsers). #1 is surely faster, but IFRAMEs have problems of their own (some platforms, e.g. Joomla!, routinely filter them out; some odd antivirus may balk or strip the iframe altogether; etc.).

Solution #3 decouples data and presentation, and might more easily be further developed in order to let users customize the widget to blend with different designs, if desired. Just my opinion, I like that one best.

A fourth possibility, depending on the widget features and specs, could be to generate a server side image - say, an (animated?) GIF. Of course this assumes the widget is nothing more than an informational passive object (one HREF at the most), so it might be out of the question.

  • Interesting points. Yeah, some of the widgets will need more interaction than static image or GIF, so that's out of the question. I suppose if I do option #3 then I can even initially embed an iframe if I want and upgrade the script later to perform more advanced DOM manipulation - and my users get seamless upgrades without needing the change their embed code.
    – Ryan Weir
    Sep 28, 2013 at 23:50

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.