Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Web Programming > JavaScript > Ajax
Ajax the combination of XHTML, CSS, DOM, XML, XSLT, XMLHttpRequest, and JavaScript
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Ajax section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
Old November 21st, 2006, 09:06 AM
Registered User
Join Date: Feb 2006
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default 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,

<!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">
<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");
        ro = new XMLHttpRequest();
    return ro;

function switchDiv(url, divID) {
    globalDIV = divID;
    //alert("Global div is " + globalDIV);
    divSwap.open('GET', url, true)
    divSwap.onreadystatechange = switchResponse;

function switchResponse() {

    if(divSwap.readyState == 4){
        if (divSwap.status == 200) {

function doMagic() {
    var myresponse = divSwap.responseText;
    document.getElementById(globalDIV).innerHTML=myres ponse;


<body onload="switchDiv('hello.htm', 'helloDiv');return; switchDiv('goodbye.htm', 'goodbyeDiv');">
<p><a href="javascript:switchDiv('hello.htm', 'helloDiv');">Hello</a> &nbsp; <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>

Old November 21st, 2006, 01:15 PM
joefawcett's Avatar
Wrox Author
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts

I imagine it's because the same variable is holding both instances of the XMLHttpRequest. You may need to create a new one for each call. You also seem to call switchDiv('hello.htm', 'helloDiv') twice as there's code in the script block as well as the onload handler. There is a return statement in the onload handler too, this means that the second call to switchDiv('goodbye.htm', 'goodbyeDiv') won't be processed.


Joe (Microsoft MVP - XML)

Similar Threads
Thread Thread Starter Forum Replies Last Post
Onload with AJAX Baco Intro Programming 0 June 11th, 2007 12:46 PM
Invoke onClick() function when body onLoad muklee Javascript How-To 2 March 16th, 2007 01:02 AM
iframe onLoad darkhalf Javascript 1 January 11th, 2006 04:42 PM
two script onLoad mateenmohd Javascript 2 December 28th, 2003 12:12 AM
Body Onload Function Question apd8x Javascript 2 July 10th, 2003 02:52 AM

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.