473,406 Members | 2,769 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Simple AJAX - Why won't this work ?

I need to add content to two DIVs on page load but this just won't
work. If I call any one function, things work fine or if I trigger the
events on-click, it works perfectly. But with both together on load,
nothing happens. Please help.

Krishna.

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Hello, World!</title>
<script type="text/javascript">

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) {
divSwap = createR();
globalDIV = divID;
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>
<p><a href="javascript:switchDiv('hello.htm', 'helloDiv');">Hello</a>
&nbsp; <a href="javascript:switchDiv('goodbye.htm',
'goodbyeDiv');">Goodbye</a></p>
<h1>krishna waz ere..</h1>
<div id="helloDiv"></div>
<h1>juss checking macha</h1>
<div id="goodbyeDiv"></div>
</body>
</html>

Nov 23 '06 #1
1 1171

cookieplanter wrote:
I need to add content to two DIVs on page load but this just won't
work. If I call any one function, things work fine or if I trigger the
events on-click, it works perfectly. But with both together on load,
nothing happens. Please help.

Krishna.

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Hello, World!</title>
<script type="text/javascript">

divSwap = createR();
var globalDIV;

switchDiv('hello.htm', 'helloDiv');
switchDiv('goodbye.htm', 'goodbyeDiv');
Your function calls aren't happening onload, they are occurring as soon
as the script is parsed and likely before the elements are ready. Use
something like:

window.onload = function(){
switchDiv('hello.htm', 'helloDiv');
switchDiv('goodbye.htm', 'goodbyeDiv');
}

function createR() {
var ro;
var browser = navigator.appName;
if(browser == "Microsoft Internet Explorer"){
Browser sniffing as soooo passé, use something based on feature
detection:

<URL: http://jibbering.com/2002/4/httprequest.html >

or use a library that does the hard bit for you:

<URL: http://www.ajaxtoolbox.com/ >

--
Rob

Nov 23 '06 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: needin4mation | last post by:
I understand this is a asp.net group, but thought I would post this here for comments. I admit I have used this post in another group, but it has less traffic. Here's to hoping I'm just blind to...
2
by: Alex | last post by:
Example uploaded to: http://www.clickatus.com/ajax/ BTW - This is for FIREFOX, won't work in IE. I don't know why but when it is executed the browser still in loading state... Even though...
8
by: darrel | last post by:
I'm going to be diving into ASP.net 2.0 on a small project. I'd also like to use it as an excuse to start playing with some AJAX techniques. So, that leads to the obvious question: AJAX.net vs....
1
by: www.web20developers.com | last post by:
http://www.web20developers.com http://www.web20developers.com/index.php?option=com_content&task=view... Ajallerix : AJAX, simple, fast Web image gallery demo ; at Novell AJAX -...
9
by: RBM007 | last post by:
Hello, I have created some pages in the (old) Atlas pages and migrated to AJAX version. After the update I noticed that any page containing ASP.NET AJAX won't compile anymore. Even if the same...
8
by: Bill Gower | last post by:
I have a webapp that uses the AjaxControlToolkit. The app and ajax works fine when run within my dev server in Visual Studio 2005 but does not work on IIS. Any Suggestions? Bill
4
by: RichardOnRails | last post by:
Hi, First, is this an appropriate NG for talking about debugging JavaScript in Aptana? Assuming it is, I'm trying to get a script to read an external text file and display the text somehow....
6
by: John Doe | last post by:
Here's my issue: I have an instant messenger type feature for my site, its basically an ajax IM feature. I run with a mysql backend on the site, i have a div on my main page that runs a javascript...
7
by: Joe | last post by:
I added some ajax to my asp.net web site and will Ajax just doesn't seem to work. Does Any one PLEASE have any ideas on why Ajax doesn't work on Apache 2.2.6 w/asp.net 2.0
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.