Skip to content

Commit

Permalink
Run embed script after DOMContentLoaded
Browse files Browse the repository at this point in the history
  • Loading branch information
ireade committed May 14, 2018
1 parent fc7d8f6 commit 46c30d5
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 40 deletions.
2 changes: 1 addition & 1 deletion caniuse-embed.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion embed/script.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "caniuse-embed",
"version": "1.2.0",
"version": "1.2.1",
"description": "The embed for caniuse",
"main": "caniuse-embed.js",
"scripts": {
Expand Down
75 changes: 38 additions & 37 deletions src/caniuse-embed.js
Original file line number Diff line number Diff line change
@@ -1,56 +1,57 @@
(function() {
document.addEventListener("DOMContentLoaded", function() {

var caniuse_embeds = document.getElementsByClassName("ciu_embed");
var caniuse_embeds = document.getElementsByClassName("ciu_embed");

for (var i = 0; i < caniuse_embeds.length; i++) {
for (var i = 0; i < caniuse_embeds.length; i++) {

var embed = caniuse_embeds[i];
var feature = embed.getAttribute('data-feature');
var periods = embed.getAttribute('data-periods');
var accessibleColours = embed.getAttribute('data-accessible-colours') || 'false';
var embed = caniuse_embeds[i];
var feature = embed.getAttribute('data-feature');
var periods = embed.getAttribute('data-periods');
var accessibleColours = embed.getAttribute('data-accessible-colours') || 'false';

if (feature) {
if (feature) {

var url = 'https://caniuse.bitsofco.de/embed/index.html';
//var url = 'http://localhost:8000/embed/index.html'
var url = 'https://caniuse.bitsofco.de/embed/index.html';
//var url = 'http://localhost:8000/embed/index.html'

var iframe = '<iframe src="'+url+'?feat='+feature+'&periods='+periods+'&accessible-colours='+accessibleColours+'" frameborder="0" width="100%" height="400px"></iframe>';
var iframe = '<iframe src="'+url+'?feat='+feature+'&periods='+periods+'&accessible-colours='+accessibleColours+'" frameborder="0" width="100%" height="400px"></iframe>';

embed.innerHTML = iframe;
embed.innerHTML = iframe;

} else {
} else {

embed.innerHTML = "A feature was not included. Go to <a href='https://caniuse.bitsofco.de/#how-to-use'>https://caniuse.bitsofco.de/#how-to-use</a> to generate an embed.";
}
}
embed.innerHTML = "A feature was not included. Go to <a href='https://caniuse.bitsofco.de/#how-to-use'>https://caniuse.bitsofco.de/#how-to-use</a> to generate an embed.";
}
}


// GET RESPONSIVE HEIGHT PASSED FROM IFRAME

// GET RESPONSIVE HEIGHT PASSED FROM IFRAME
var eventMethod = window.addEventListener ? "addEventListener" : "attachEvent";
var eventer = window[eventMethod];
var messageEvent = eventMethod == "attachEvent" ? "onmessage" : "message";

var eventMethod = window.addEventListener ? "addEventListener" : "attachEvent";
var eventer = window[eventMethod];
var messageEvent = eventMethod == "attachEvent" ? "onmessage" : "message";
eventer(messageEvent,function(e) {
var data = e.data;
if ( (typeof data === 'string') && (data.indexOf('ciu_embed') > -1) ) {

eventer(messageEvent,function(e) {
var data = e.data;
if ( (typeof data === 'string') && (data.indexOf('ciu_embed') > -1) ) {
var featureID = data.split(':')[1];
var height = data.split(':')[2];

var featureID = data.split(':')[1];
var height = data.split(':')[2];
for (var i = 0; i < caniuse_embeds.length; i++) {

for (var i = 0; i < caniuse_embeds.length; i++) {
var embed = caniuse_embeds[i];

if ( embed.getAttribute('data-feature') === featureID ) {
var iframeHeight = parseInt(height) + 30;
embed.childNodes[0].height = iframeHeight + 'px';
break;
}
}
}
},false);

var embed = caniuse_embeds[i];

if ( embed.getAttribute('data-feature') === featureID ) {
var iframeHeight = parseInt(height) + 30;
embed.childNodes[0].height = iframeHeight + 'px';
break;
}
}
}
},false);
}); // end DOMContentLoaded


}).call(this);
}());

0 comments on commit 46c30d5

Please sign in to comment.