Second onload function does not work

2019-08-04 00:06发布

问题:

I have the following code and the second onload event is not firing:

    <script type="text/javascript">
    var startime = (new Date()).getTime();

    window.onload = function(){ record_visit('ol'); } //ol - onload
    window.onload = function(){ setInterval("upState()", 30000); }
    window.onbeforeunload = function(){ record_visit('obul'); } //obul = onbeforeunload

    function record_visit(value) {
        var x = (window.ActiveXObject) ? new ActiveXObject('Microsoft.XMLHTTP') : new XMLHttpRequest();
        x.open("GET", "count_visit.php?t=" + (((new Date()).getTime() - startime) / 1000)+"&type="+value+"&url="+escape(window.location.href), false);
        x.send(null);
    }

    function upState()
    {
        // create the AJAX variable
        var xmlhttp;
        if (window.XMLHttpRequest)
            xmlhttp = new XMLHttpRequest();
        else
            xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");

        // make the AJAX call
        xmlhttp.open("GET", "count_visit.php?t=" + (((new Date()).getTime() - startime) / 1000)+"&type=update&url="+escape(window.location.href), false);
        x.send(null);
    }   
</script>

All I need here is to send request using count_visit.php and update the table that the visitor is still online.

I tried some code I found in some site but still it's not firing. here's the code:

    function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      if (oldonload) {
        oldonload();
      }
      func();
    }
  }
}
addLoadEvent(record_visit('ol'));
addLoadEvent(setInterval("upState()", 30000));

Any help please.

回答1:

With the second window.onload you are overwriting the one that was defined before. As you are not using jQuery (which makes these things easier) use the following function

function myEvent(where,evt,func,op)
{
 if (op)
  {
   if (where.attachEvent) where.attachEvent("on"+evt,func);
   else if (where.addEventListener) where.addEventListener(evt,func,false)
  }
  else 
  {
   if (where.detachEvent) where.detachEvent("on"+evt,func);
   else if (where.removeEventListener) where.removeEventListener(evt,func,false);
  }
}

where - object where you are adding your event listener to

evt - the name of the event (without 'on' part)

func - function to be called on event

op - if it is set to true the function will add listener and if it is set to false the function will remove listener.

Call it as myEvent(window, 'load', func, true); And does not matter how many functions you add - all of them will be called at the given event.

ps: or you can just combine the content of both functions manually :))

window.onload = function(){ 
      record_visit('ol'); 
      setInterval(upState, 30000); 
}

using this method you have to check the existence of the previous event handler, save it in some global variable and call it later when you will be dealing with execution of the final and the only one window.onload function. You were trying to do it in the last portion of the code.



回答2:

if you want two functions to fire on window.onload, you could do it like this:

function onload1 (){
    alert("onload1");
}
function onload2 (){
    alert("onload2");
}
window.onload = function() {
    onload1();
    onload2();
}