For smart Primates & ROBOTS (oh and ALIENS ).

Blogroll

Friday, April 07, 2017

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>
 



Share:

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.

Share:

Multiple attribute passing in querySelectorAll

Multiple attribute passing in querySelectorAll     Here I am demonstrating code to how to pass multiple attributes in querySelectorAll. <...

Ads Inside Post

Powered by Blogger.

Arsip