Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

adding an event listener 1

Status
Not open for further replies.

1DMF

Programmer
Jan 18, 2005
8,795
GB
I've been searching for a simple cross-browser way of adding an event listner.

I've found these two methods, which is the best or are both required for cross browser functionality...

Code:
obj.attachEvent( 'onload', 'myfunc' );

[b]or[/b]

obj.addEventListener( 'onload', 'myfunc', false );


"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you."

"If a shortcut was meant to be easy, it wouldn't be a shortcut, it would be the way!
 
ok, found that GOAN doesn't work anymore if you use the file, however their does work.

so now i'm using this code...
Code:
<script type="text/javascript">

var GAscript=null;

function mygat()
{
  if (typeof(_gat) == 'undefined') {
    if(GAscript!=null){
        GAscript.parentNode.removeChild(GAscript);        
    }
    
    GAscript=document.createElement('script')
    GAscript.src='[URL unfurl="true"]https://ssl.google-analytics.com/ga.js'[/URL]
    GAscript.type='text/javascript'
    document.getElementsByTagName('head')[0].appendChild(GAscript)
    window.setTimeout(mygat,1000);
  } 
  else {
    var pageTracker = _gat._getTracker("UA-XXXXXXX-X");
    pageTracker._trackPageview();
  }
}

function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } 
  else {
    window.onload = function() {
      if(oldonload){oldonload();}
      func();
    }
  }
}

addLoadEvent(mygat);

</script>

"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you."

"If a shortcut was meant to be easy, it wouldn't be a shortcut, it would be the way!
 
oh and that also confirms that the onload event does NOT wait for things to load, if it can't get the external scripts or other source files it ignores them and carries on!

"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you."

"If a shortcut was meant to be easy, it wouldn't be a shortcut, it would be the way!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top