Skip to content

Instantly share code, notes, and snippets.

@kimjoar
Created July 29, 2010 11:15
Show Gist options
  • Select an option

  • Save kimjoar/497860 to your computer and use it in GitHub Desktop.

Select an option

Save kimjoar/497860 to your computer and use it in GitHub Desktop.

"Best practice" for Javascript widgets: Doesn't use any libraries, doesn't pollute the global namespace, it's cross-browser (to the best of my knowledge), and it's very small. Inspired by Get Satisfaction and Emil Stenström.

(function() {
function add_iframe() {
var iframe = document.createElement('iframe');
iframe.src = 'http://example.org/widget.html';
iframe.width = "300";
iframe.height = "200";
var el = document.getElementById("widget");
el.appendChild(iframe)
}
if (window.attachEvent) {
window.attachEvent('onload', add_iframe);
} else {
window.addEventListener('load', add_iframe, false);
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment