Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add script element dynamically on Blackberry browser?

I am on blackberry9300 (5.0.0.832). And I am trying to add a javascript element programmatically.

var script=document.createElement('script');
script.src='file.js';
script.type='text/javascript';
script.onload=function(){alert('loaded');}; //Nothing happens
script.addEventListener('load', function(){alert('loaded');}, false); //Nothing happens
script.addEventListener('readystatechange', function(){alert('state changed');}, false); //Nothing happens
script.onreadystatechange=function(){alert('state changed')}; //Nothing happens
document.getElementsByTagName('head').item(0).appendChild(script);
Is there any way to workaround this issue?
like image 729
bhups Avatar asked Feb 21 '26 11:02

bhups


1 Answers

blackberry browser on older os (< 6.0) doesn't fire onload events or doesn't change readyState of script. So I need to poll if js file is loaded successfully or not. Here is that function if anybody is interested.

function loadScript(path,callback,chkvar){
  var a=document.createElement('script');
  a.src=path;
  a.type='text/javascript';
  a.onreadystatechange=function(){
    if(this.readyState=='complete'||this.readyState=='loaded')
      callback();
  };
  a.onload=callback;
  if(a.hasOwnProperty('onload')==false)
    var e=setInterval(function(){
      if(eval(chkvar)){  //chkvar will only be available after js file is loaded.
        clearInterval(e);
        callback();
      }
    },50);
  }
  document.getElementsByTagName('head').item(0).appendChild(a);
}
like image 88
bhups Avatar answered Feb 24 '26 06:02

bhups



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!