var imp_per_fire = 10; if (!window.jQuery) { var jq = document.createElement('script'); jq.type = 'text/javascript'; // Path to jquery.js file, eg. Google hosted version jq.src = 'https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js'; document.getElementsByTagName('head')[0].appendChild(jq); console.warn('fored jquery'); } $(document).ready(collectdiv); function collectdiv( jQuery ) { var newArray = new Array(); if($("div.ait-imp").length) { $("div.ait-imp").each(function(){ var href = $(this).attr("data-href"); var tag = $(this).attr("tag"); var o = { 'href': href, 'tag': tag }; newArray.push(o); }); $.ajax({ type: "POST", url: "https://api.upower.com.hk/AITshowimpcore.php", dataType: 'json', data: { json: JSON.stringify(newArray) }, success: function(data){filloutput(data);} }); }else{ console.log('no div.ait-imp found'); } } function filloutput(data) { var idx = 0; $("div.ait-imp").each(function(){ switch($(this).attr("data-layout")) { case "fire_count": var imp = Number(data.impposts[idx].impression); if ($(this).attr('data-imp')){ imp=$(this).attr('data-imp'); $(this).removeAttr('data-imp'); } switch(true) { case (imp <= imp_per_fire*1): $(this).html(""); break; case (imp <= imp_per_fire*2): $(this).html(""); break; case (imp <= imp_per_fire*3): $(this).html(""); break; case (imp <= imp_per_fire*4): $(this).html(""); break; case (imp > imp_per_fire*4): $(this).html(""); break; } break; default: $(this).html(""+ data.impposts[idx].impression +""); break; } idx++; }); }