(function () { var scriptName = "embed.js"; //name of this script, used to get reference to own tag var jQuery; //noconflict reference to jquery var jqueryPath = "https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"; var jqueryVersion = "1.8.3"; var scriptTag; //reference to the html script tag /******** Get reference to self (scriptTag) *********/ var allScripts = document.getElementsByTagName('script'); var targetScripts = []; for (var i in allScripts) { var name = allScripts[i].src if(name && name.indexOf(scriptName) > 0) targetScripts.push(allScripts[i]); } scriptTag = targetScripts[targetScripts.length - 1]; /******** helper function to load external scripts *********/ function loadScript(src, onLoad) { var script_tag = document.createElement('script'); script_tag.setAttribute("type", "text/javascript"); script_tag.setAttribute("src", src); if (script_tag.readyState) { script_tag.onreadystatechange = function () { if (this.readyState == 'complete' || this.readyState == 'loaded') { onLoad(); } }; } else { script_tag.onload = onLoad; } (document.getElementsByTagName("head")[0] || document.documentElement).appendChild(script_tag); } /******** helper function to load external css *********/ function loadCss(href) { var link_tag = document.createElement('link'); link_tag.setAttribute("type", "text/css"); link_tag.setAttribute("rel", "stylesheet"); link_tag.setAttribute("href", href); (document.getElementsByTagName("head")[0] || document.documentElement).appendChild(link_tag); } /******** load jquery into 'jQuery' variable then call main ********/ if (window.jQuery === undefined || window.jQuery.fn.jquery !== jqueryVersion) { loadScript(jqueryPath, initjQuery); } else { initjQuery(); } function initjQuery() { jQuery = window.jQuery.noConflict(true); main(); } /******** starting point for your widget ********/ function main() { //your widget code goes here //configuracoes aqui // console.log('SM INIT -> '); var SM_RemoteUrl = 'https://smonitor.com.br' // var SM_RemoteUrl = 'http://localhost:12080' var SM_Widget = "uai-r"; var SM_Quantidade = "4" ; var SM_Tipo = "link" ; var SM_Rank = "tx" ; var SM_LookBack = "12" loadCss(SM_RemoteUrl + "/static/" + SM_Widget + ".css"); //example jsonp call var jsonp_url = SM_RemoteUrl + "/widget/?site=" + SM_Widget + "&qtd=" + SM_Quantidade + "&tipo=" + SM_Tipo + "&rank=" + SM_Rank; jQuery.getJSON(jsonp_url, function(result) { // console.log('SM window width -> ' + $(window).width()); var SM_window = $(window).width(); jQuery('#sm-widget-container').html(result.html); var IntRandom = Math.floor((Math.random() * 100) + 1); // console.log('SM PercentAds -> ' + result.PercentAds); // console.log('SM IntRandom -> ' + IntRandom); if (SM_window < 500) { // console.log('SM Display CustumerAds'); // jQuery('#sm-shared-ads').html(result.CustumerAds); } else { console.log('SM Display MyAds'); // console.log('SM ' + result.MyAds); // jQuery('#sm-shared-ads').html(result.MyAds); } // console.log("SM Url = " + document.location); // console.log("SM PathName = " + window.location.pathname);// Returns path only // console.log("SM url = " + window.location.href);// Returns full URL // console.log("SM load ok");// Returns full URL }); jQuery(document).ready(function ($) { //loadScript("http://example.com/anotherscript.js", function() { /* loaded */ }); }); } })();