Hello,

Answer me I am adding multiple function onload in the body.

IE showing the correct result but mozilla showing the problem like it showing the last function result in all function results.


<body id="body" onload="showUser1('taj-mahal');showUser('taj-mahal');showUser2('taj-mahal');">

can anybody help on this,what is the problem???

and how to short out

Dani AI

Generated

Calling several functions from an onload attribute is valid HTML and browsers will execute the statements in order. In 's example the onload line itself is not the likely cause of Firefox showing the "last result" for every call. 's wrapper suggestion is a good way to make the sequence explicit and easier to reason about; 's nested-call idea is another option. The cross-browser difference usually points to shared state or asynchronous callbacks inside the functions rather than the onload attribute itself. was correct that this belongs in a JavaScript discussion rather than PHP.

Common root causes when multiple AJAX-style functions appear to return the same result:

  • Reusing a single global XMLHttpRequest or a single global response handler for several requests, so later responses overwrite earlier handlers.
  • Closure/loop-variable capture problems (classic var in loops) causing callbacks to see the final loop value.
  • Writing every response into the same DOM node without distinguishing which response belongs where.

Practical fixes: give each request its own XHR and handler, capture per-call values with a closure or use ES6 let in loops, and insert results into distinct target elements or include an id with each response. Minimal XHR pattern to keep the handler local:

var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function () {
  if (xhr.readyState === 4 && xhr.status === 200) {
    // handle response specific to this request
  }
};
xhr.open('GET', url, true);
xhr.send(null);

Debugging tips: use Firefox Developer Tools (Console and Network) to confirm which requests run and what each response contains. To attach multiple independent handlers prefer addEventListener('load', fn) over stuffing many statements into a single onload attribute (addEventListener - MDN). For AJAX details see XMLHttpRequest - MDN.

Recommended Answers

All 3 Replies

Why dont you create a fuction to call all of this functions like this

function allfunction(){
showUser1('taj-mahal');
showUser('taj-mahal');
showUser2('taj-mahal');
}
<body id="body" onload="allfunction();">

and for your info your posting a javascript thread in a php thread

Yes, the above idea is good. Other wise try to call a function inside a function.
for example.

function ss()
{
var a=10;
var c=11;
vard=a+b;

function sss();
}

Member Avatar for Member #120589

Listen, why don't you guys find a room? preferrably a JS ROOM.

This is pHp. Sort it out.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.