Skip to content

Instantly share code, notes, and snippets.

@nuc
Last active December 26, 2015 21:59
Show Gist options
  • Select an option

  • Save nuc/7220244 to your computer and use it in GitHub Desktop.

Select an option

Save nuc/7220244 to your computer and use it in GitHub Desktop.
iFrame bridge for IE9 cors support (abstracted from moviepilot.com)
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<script>document.domain = "moviepilot.com"</script>
</head>
<body></body>
</html>
<!DOCTYPE html>
<meta charset="utf-8">
<head>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script type="text/javascript">
$(function() {
jQuery.support.cors = true;
document.domain = 'moviepilot.com';
iframe = document.createElement("iframe");
iframe.setAttribute("src", "http://api.moviepilot.com/bridge.html");
iframe.attachEvent('onload', function() {
$('div').html('iframe loaded');
var context = iframe.contentWindow;
var xhr = function() {
return new context.XMLHttpRequest();
};
var params = {
dataType: 'json',
type: 'GET',
url: 'http://api.moviepilot.com/v2',
xhr: xhr
};
if (jQuery.support.cors) {
params.crossDomain = true;
params.xhrFields = {
withCredentials: true
};
}
if (params.crossDomain) {
if (params.type === 'PUT' || params.type === 'DELETE') {
var type = params.type;
params.type = 'POST';
params.beforeSend = function(xhr) {
return xhr.setRequestHeader('X-HTTP-Method-Override', type);
};
}
}
if (params.dataType === 'json' && params.type !== 'GET') {
params.contentType = 'application/json; charset=UTF-8';
if (typeof params.data == 'object') {
params.data = JSON.stringify(params.data);
}
}
var promise = $.ajax(params);
promise.fail(function() {
$('div').html('cors request error');
});
promise.success(function(data, status) {
$('div').html("API Version: " + data.version);
});
});
iframe.attachEvent('onerror', function() {
$('div').html('iframe load error');
});
return document.body.appendChild(iframe);
});
</script>
</head>
<body>
<div></div>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment