From adac9121266aece68c5b5127ca7e1401d55c60b1 Mon Sep 17 00:00:00 2001 From: Ire Aderinokun Date: Mon, 25 Jan 2016 10:18:06 +0100 Subject: [PATCH] display error message if no feature or period passed --- caniuse-embed.min.js | 2 +- development/embed/script.js | 18 ++++++++++++++---- embed/script.js | 2 +- 3 files changed, 16 insertions(+), 6 deletions(-) diff --git a/caniuse-embed.min.js b/caniuse-embed.min.js index 3083470..485bb6d 100644 --- a/caniuse-embed.min.js +++ b/caniuse-embed.min.js @@ -1 +1 @@ -(function(){for(var e=document.getElementsByClassName("ciu_embed"),t=0;t';a.innerHTML=d}else a.innerHTML="A feature was not included. Go to http://caniuse.bitsofco.de/#how-to-use to generate an embed."}var s=window.addEventListener?"addEventListener":"attachEvent",o=window[s],f="attachEvent"==s?"onmessage":"message";o(f,function(t){var a=t.data;if("string"==typeof a&&a.indexOf("ciu_embed")>-1)for(var i=a.split(":")[1],r=a.split(":")[2],n=0;n';s.innerHTML=o}else s.innerHTML="A feature was not included. Go to http://caniuse.bitsofco.de/#how-to-use to generate an embed."}var i=window.addEventListener?"addEventListener":"attachEvent",d=window[i],l="attachEvent"==i?"onmessage":"message";d(l,function(t){var s=t.data;if("string"==typeof s&&s.indexOf("ciu_embed")>-1)for(var a=s.split(":")[1],n=s.split(":")[2],r=0;rCan I Use '+featureID+'? Data on support for the '+featureID+' feature across the major browsers from caniuse.com. (Embed Loading)'; + +} else { + document.getElementById('defaultMessage').innerHTML = 'Error: Feature and/or Periods not Specified'; +} + + + + + // ADD TABLE ROWS FOR EACH PERIOD @@ -59,8 +70,7 @@ function loadJSON(path, success, error) { } -// SET DEFAULT MESSAGE OF EMBED LOADING -document.getElementById('defaultMessage').innerHTML = 'Can I Use '+featureID+'? Data on support for the '+featureID+' feature across the major browsers from caniuse.com. (Embed Loading)'; + diff --git a/embed/script.js b/embed/script.js index b586f40..50aa4a2 100644 --- a/embed/script.js +++ b/embed/script.js @@ -1 +1 @@ -function getShortenedBrowserVersion(e){return e&&e.indexOf("-")>-1&&(e=e.split("-")[1]),e}function loadJSON(e,t,s){var n=new XMLHttpRequest;n.onreadystatechange=function(){n.readyState===XMLHttpRequest.DONE&&(200===n.status?t&&t(JSON.parse(n.responseText)):s&&s(n))},n.open("GET",e,!0),n.send()}for(var caniuseDataUrl="https://raw.githubusercontent.com/Fyrd/caniuse/master/fulldata-json/data-2.0.json",featureID=location.href.split("?feat=")[1],featureID=featureID.split("&periods=")[0],periods=location.href.split("&periods=")[1],periods=periods.split(","),browsers=["ie","edge","firefox","chrome","safari","opera","ios_saf","op_mini","android","and_chr"],i=periods.length-1;i>-1;i--){var tableCells='',row=document.createElement("tr");row.className="statistics "+periods[i],row.innerHTML=tableCells,document.getElementById("tableBody").appendChild(row)}document.getElementById("defaultMessage").innerHTML='Can I Use '+featureID+"? Data on support for the "+featureID+" feature across the major browsers from caniuse.com. (Embed Loading)",loadJSON(caniuseDataUrl,function(e){var t=e.data[featureID];if(t){var s=t.usage_perc_y,a=t.usage_perc_a,r=s+a,r=r.toFixed(2);document.getElementById("featureTitle").innerHTML=t.title,document.getElementById("featureDescription").innerHTML=t.description,document.getElementById("featureLink").href="http://caniuse.com/#feat="+featureID,document.getElementById("note").innerHTML='Global: '+s+'% + '+a+"% = "+r+"%";for(var o={},i=0;i-1?(n=parseInt(f.split("_")[1]),o[l][f]=e.agents[l].version_list[d-n]?e.agents[l].version_list[d-n].version:null):f.indexOf("future")>-1&&(n=parseInt(f.split("_")[1]),o[l][f]=e.agents[l].version_list[d+n]?e.agents[l].version_list[d+n].version:null)}}for(var p={},i=0;i-1&&(b=w[E]);void 0!=h[l][f]?b.className+=" "+h[l][f]:!1;var _=getShortenedBrowserVersion(o[l][f]),B=""+_+''+p[l][f]+"%";void 0!=o[l][f]?b.innerHTML=B:b.innerHTML="",void 0!=h[l][f]&&h[l][f].indexOf("x")>-1&&(v=!0),void 0!=h[l][f]&&h[l][f].indexOf("u")>-1&&(y=!0)}v?document.getElementById("legendX").style.display="inline-block":document.getElementById("legendX").style.display="none",y?document.getElementById("legendU").style.display="inline-block":document.getElementById("legendU").style.display="none"}else document.getElementById("featureTitle").innerHTML="Uh Oh!",document.getElementById("featureDescription").innerHTML="The feature '"+featureID+"' was not recognized. ",document.getElementById("featureMain").innerHTML="";document.getElementById("defaultMessage").style.display="none",document.getElementsByClassName("feature")[0].style.display="block";var M=document.getElementsByClassName("feature")[0].scrollHeight,L="ciu_embed:"+featureID+":"+M;parent.postMessage(L,"*"),window.onresize=function(e){M=document.getElementsByClassName("feature")[0].scrollHeight;var t="ciu_embed:"+featureID+":"+M;parent.postMessage(t,"*")}},function(e){document.getElementById("defaultMessage").innerHTML="Error Getting JSON File: "+e.response,console.error(e)}); \ No newline at end of file +function getShortenedBrowserVersion(e){return e&&e.indexOf("-")>-1&&(e=e.split("-")[1]),e}function loadJSON(e,t,s){var n=new XMLHttpRequest;n.onreadystatechange=function(){n.readyState===XMLHttpRequest.DONE&&(200===n.status?t&&t(JSON.parse(n.responseText)):s&&s(n))},n.open("GET",e,!0),n.send()}var caniuseDataUrl="https://raw.githubusercontent.com/Fyrd/caniuse/master/fulldata-json/data-2.0.json",featureID=location.href.split("?feat=")[1],featureID=featureID?featureID.split("&periods=")[0]:null,periods=location.href.split("&periods=")[1],periods=periods?periods.split(","):null,browsers=["ie","edge","firefox","chrome","safari","opera","ios_saf","op_mini","android","and_chr"];featureID&&periods?document.getElementById("defaultMessage").innerHTML='Can I Use '+featureID+"? Data on support for the "+featureID+" feature across the major browsers from caniuse.com. (Embed Loading)":document.getElementById("defaultMessage").innerHTML="Error: Feature and/or Periods not Specified";for(var i=periods.length-1;i>-1;i--){var tableCells='',row=document.createElement("tr");row.className="statistics "+periods[i],row.innerHTML=tableCells,document.getElementById("tableBody").appendChild(row)}loadJSON(caniuseDataUrl,function(e){var t=e.data[featureID];if(t){var s=t.usage_perc_y,a=t.usage_perc_a,r=s+a,r=r.toFixed(2);document.getElementById("featureTitle").innerHTML=t.title,document.getElementById("featureDescription").innerHTML=t.description,document.getElementById("featureLink").href="http://caniuse.com/#feat="+featureID,document.getElementById("note").innerHTML='Global: '+s+'% + '+a+"% = "+r+"%";for(var o={},i=0;i-1?(n=parseInt(f.split("_")[1]),o[l][f]=e.agents[l].version_list[d-n]?e.agents[l].version_list[d-n].version:null):f.indexOf("future")>-1&&(n=parseInt(f.split("_")[1]),o[l][f]=e.agents[l].version_list[d+n]?e.agents[l].version_list[d+n].version:null)}}for(var p={},i=0;i-1&&(b=E[w]);void 0!=h[l][f]?b.className+=" "+h[l][f]:!1;var _=getShortenedBrowserVersion(o[l][f]),B=""+_+''+p[l][f]+"%";void 0!=o[l][f]?b.innerHTML=B:b.innerHTML="",void 0!=h[l][f]&&h[l][f].indexOf("x")>-1&&(v=!0),void 0!=h[l][f]&&h[l][f].indexOf("u")>-1&&(y=!0)}v?document.getElementById("legendX").style.display="inline-block":document.getElementById("legendX").style.display="none",y?document.getElementById("legendU").style.display="inline-block":document.getElementById("legendU").style.display="none"}else document.getElementById("featureTitle").innerHTML="Uh Oh!",document.getElementById("featureDescription").innerHTML="The feature '"+featureID+"' was not recognized. ",document.getElementById("featureMain").innerHTML="";document.getElementById("defaultMessage").style.display="none",document.getElementsByClassName("feature")[0].style.display="block";var M=document.getElementsByClassName("feature")[0].scrollHeight,L="ciu_embed:"+featureID+":"+M;parent.postMessage(L,"*"),window.onresize=function(e){M=document.getElementsByClassName("feature")[0].scrollHeight;var t="ciu_embed:"+featureID+":"+M;parent.postMessage(t,"*")}},function(e){document.getElementById("defaultMessage").innerHTML="Error Getting JSON File: "+e.response,console.error(e)}); \ No newline at end of file