onload function calll
Hello all,
I am having a Javascript function named "switchDiv(url, divID)", that uses the XMLHttpRequest object to open a file (url) and place the file contents in a DIV with id (divID). I am calling this function onload of the document,
its working fine if i call this function once.
But when i want to call this function twice with different url and divID its not working.
It works if i use:
<body onload="switchDiv('hello.htm', 'helloDiv');">
Its not working if i use:
<body onload="switchDiv('hello.htm', 'helloDiv');switchDiv('goodbye.htm', 'goodbyeDiv');>
I am attaching the code for reference
Please help me out,
Thanks in advance,
Tanveer
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Hello, World!</title>
<script type="text/javascript">
var divSwap = createR();
var globalDIV;
switchDiv('hello.htm', 'helloDiv');
switchDiv('goodbye.htm', 'goodbyeDiv');
function createR() {
var ro;
var browser = navigator.appName;
if(browser == "Microsoft Internet Explorer"){
ro = new ActiveXObject("Microsoft.XMLHTTP");
}else{
ro = new XMLHttpRequest();
}
return ro;
}
function switchDiv(url, divID) {
globalDIV = divID;
//alert("Global div is " + globalDIV);
divSwap.open('GET', url, true)
divSwap.onreadystatechange = switchResponse;
divSwap.send(null);
}
function switchResponse() {
if(divSwap.readyState == 4){
if (divSwap.status == 200) {
doMagic();
}
}
}
function doMagic() {
var myresponse = divSwap.responseText;
document.getElementById(globalDIV).innerHTML=myres ponse;
}
</script>
</head>
<body onload="switchDiv('hello.htm', 'helloDiv');return; switchDiv('goodbye.htm', 'goodbyeDiv');">
<p><a href="javascript:switchDiv('hello.htm', 'helloDiv');">Hello</a> <a href="javascript:switchDiv('goodbye.htm', 'goodbyeDiv');">Goodbye</a></p>
<h1>DIV one</h1>
<div id="helloDiv"></div>
<h1>DIV two</h1>
<div id="goodbyeDiv"></div>
</body>
</html>
|