Skip to content

Instantly share code, notes, and snippets.

@caridy
Created November 17, 2011 16:56
Show Gist options
  • Save caridy/1373730 to your computer and use it in GitHub Desktop.
Save caridy/1373730 to your computer and use it in GitHub Desktop.
fixing the dynamic iframe's domain to solve the cross domain issue in IE when running in a page with a canonical domain defined.
// This is pure craziness, injecting an iframe in a page that has
// a fixed canonical domain will cause the dynamic iframe to fail
// to access the parent window.
// As a workaround, we can fix the domain in the dynamic iframe for IE.
(function() {
var inject = function(d, w) {
var ie = w.navigator.userAgent.match(/MSIE\s/),
i = d.createElement("iframe"),
n = (ie && (d.domain != d.location.host) ? ("<scri" + "pt>try{document.domain='#d';}catch(e){}</scr" + "ipt>").replace("#d", d.domain) : ""),
f = ("<!doctype html><html><head>" + n + "<sc" + "ript src='#c'></scri" + "pt></head><body></body></html>").replace("#c", "http://your-company.com/your-app-controller.js"),
s = ("javascript:window.onload=function(){document.open().write(\"#f\");document.close();};").replace("#f", f);
ie && i.setAttribute("src", s);
i.setAttribute("frameBorder", "0");
i.setAttribute("name", "controller");
i.style.cssText = "position:absolute;width:1px;height:1px;left:-9999px;";
if (d.body) {
d.body.appendChild(i);
} else {
d.appendChild(i);
}
if (!ie) {
d = i.contentWindow.document;
d.open().write(f);
d.close();
}
},
bookmarklet = document.getElementById('bookmarklet');
inject(document, window);
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment