views:

630

answers:

3

Okay, now I've got this thing I need to do with Javascript but I have no idea the best way of doing it! I'll explain by telling you how I "think" it should work...

  1. Inside the web page there will be multiple span tag like this: <span onload="myfunction(1);"></span>

  2. myfunction() (hosted externally) will wait until the page finishes loading and collect all the information from each of the onload events.

  3. Then myfunction() will send this information to a external php page for processing.

  4. The php page will return some html. (Probably in an array because I want to do this in 1 call)

  5. myfunction() will then replace the span tags with the html.

Steps 3, 4, and 5 I know how to do but steps 1 and 2 I'm not sure how to achieve? I've listed all my steps here just in case someone sees another big problem I might run into.

A: 

The span tag does not have the "onload"

andres descalzo
A: 

You can try to use a code similar to the next

function collector(){
  var spans = document.getElementsByTagName("SPAN");
  var commands = [];
  for (var i=0; i<spans.length; i++)
     if (spans[i].getAttribute("onload"))
        commands.push(spans[i].getAttribute("onload"));

   //here you have all onload commands in the array and can go to stage 3
}
if (window.addEventListener)
  window.addEventListener("load",collector,false):
else
  window.attachEVent("onload",collector);
Aquatic
can you use anything for the getAttribute value?
EddyR
thank you. I can't believe it was that easy! I figured it was going to be very complicated and possibly with AJAX. I learn't a few new things with that one :)
EddyR
A: 

Since onload event is supported only by < body>, < frame>, < frameset>, < iframe>, < img>, nothing will happen. I would reccomend you put id's for every span and put also something like this:

<body onload="collector.run()">
<span id="s1"></span>
<script> collector.delayFunction("s1",/data/) </script>
<span id="s2"></span>
<script> collector.delayFunction("s2",/data/) </script>
<span id="s3"></span>
<script> collector.delayFunction("s3",/data/) </script>
<span id="s4"></span>
<script> collector.delayFunction("s4",/data/) </script>
</body>

//function in js file somewhere above
var collector=(function (){
    this.stack={};

    this.delayFunction= function(id,data){
         this.collector.stack[id]=data;
    }

    this.run=function(){// this function will process all collected data
    }
})();
Eldar Djafarov