Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > Web Programming > JavaScript > Ajax
Password Reminder
| FAQ | Members List | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old November 21st, 2006, 09:06 AM
Registered User
Join Date: Feb 2006
Location: , , .
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>

Reply With Quote
  #2 (permalink)  
Old November 21st, 2006, 01:15 PM
joefawcett's Avatar
Wrox Author
Points: 9,763, Level: 42
Points: 9,763, Level: 42 Points: 9,763, Level: 42 Points: 9,763, Level: 42
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
Join Date: Jun 2003
Location: Exeter, , United Kingdom.
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)
Reply With Quote

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

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

All times are GMT -4. The time now is 01:57 AM.

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