Ads Inside Post

Search in This Blog

Load JavaScript with extra tags for example data-shr-siteid or data-sumo-site-id

Load Load JavaScript with extra tags for example data-shr-siteid or data-sumo-site-id

You can use below code.

<script>
 var loadScript = function(location, callback){
   var fileRef = document.createElement('script');
   fileRef.setAttribute('type','text/javascript');

   if (callback) {
     if (fileRef.readyState) {  // IE
       fileRef.onreadystatechange = function() {
         if (fileRef.readyState == 'loaded' || fileRef.readyState == 'complete') {
           fileRef.onreadystatechange = null;
           callback();
         }
       };
     } else {  // Non-IE
       fileRef.onload = function(){
         callback();
       };
     }
   }

   fileRef.setAttribute('src', location);
   fileRef.setAttribute('data-shr-siteid', '<?php echo $api_key; ?>');
   fileRef.async = true;
   document.head.appendChild(fileRef);
 };

function doss() {
    var surl='<?php echo ShareaholicUtilities::asset_url('assets/pub/shareaholic.js') ?>';
 loadScript(surl, function() {
   // CALLBACK - code that does something with the data returned by loading the script
 });
}
setTimeout("doss();",6000);
</script>
 



load javascript with callback script

How to load JavaScript with callback script

Sometime you needed to write a javascript that load and after load immediately call other it's related javascript code. It is called load javascript with callback script.

How to achieve it?
It is simple. below is the few lines of code.

<script type="text/javascript">
function loadmyScript(url, callback){
    var sc = document.createElement("script")
    sc.type = "text/javascript";

    if (sc.readyState){  //IE
        sc.onreadystatechange = function(){
            if (sc.readyState == "loaded" ||
                    sc.readyState == "complete"){
                sc.onreadystatechange = null;
                callback();
            }
        };
    } else {  //Others
        sc.onload = function(){
            callback();
        };
    }

    sc.src = url;
    document.getElementsByTagName("head")[0].appendChild(sc);
}

function aa(){
loadmyScript("//www.com/javascript.js", function(){
// add here callback javascript code


});
}
aa();
</script>



below is one line of JavaScript code that check your system internet connect On of OFF.