Skip to content

Instantly share code, notes, and snippets.

@DanElliottPalmer
Last active December 10, 2015 19:48
Show Gist options
  • Save DanElliottPalmer/4483297 to your computer and use it in GitHub Desktop.
Save DanElliottPalmer/4483297 to your computer and use it in GitHub Desktop.
Bumped into a problem with IE9 not being able to use appendChild() on the body. This little snippet helped fix it.
var fnAppendToBody = (function(){
var domBody = document.body;
try {
var d = document.createElement('div');
domBody.appendChild( d );
d.parentNode.removeChild( d );
} catch(e) {
domBody = document.documentElement.getElementsByTagName('body')[0];
}
return function(domElm){
domBody.appendChild( domElm );
}
})();
@DanElliottPalmer
Copy link
Author

Switched it to get the body from documentElement. Safer I feel.

@DanElliottPalmer
Copy link
Author

Added functionality to remove the test div

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment