473,563 Members | 2,831 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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('hell o.htm', 'helloDiv');
switchDiv('good bye.htm', 'goodbyeDiv');

function createR() {
var ro;
var browser = navigator.appNa me;
if(browser == "Microsoft Internet Explorer"){
ro = new ActiveXObject(" Microsoft.XMLHT TP");
}else{
ro = new XMLHttpRequest( );
}
return ro;
}
function switchDiv(url, divID) {
divSwap = createR();
globalDIV = divID;
divSwap.open('G ET', url, true)
divSwap.onready statechange = switchResponse;
divSwap.send(nu ll);
}
function switchResponse( ) {
if(divSwap.read yState == 4) {
if (divSwap.status == 200) {
doMagic();
}
}
}
function doMagic() {
var myresponse = divSwap.respons eText;
document.getEle mentById(global DIV).innerHTML= myresponse;
}
</script>
</head>
<body>
<p><a href="javascrip t:switchDiv('he llo.htm', 'helloDiv');">H ello</a>
&nbsp; <a href="javascrip t:switchDiv('go odbye.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 1175

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('hell o.htm', 'helloDiv');
switchDiv('good bye.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('hell o.htm', 'helloDiv');
switchDiv('good bye.htm', 'goodbyeDiv');
}

function createR() {
var ro;
var browser = navigator.appNa me;
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
1766
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 the obvious: I have been reading a lot about AJAX. I want to use it and will. But I keep reading about how it doesn't make a roundtrip to the...
2
1849
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 XMLHttpRequest already got its string from the server... Any help?
8
2506
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. Atlas? Anyone have any opinions on that? It looks like Atlas is still considered 'beta' so perhaps that's one strike against it.
1
16487
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 - microlink pattern tutorial : A microlink is a link that opens up
9
2130
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 tags and properties are used in a page without a masterpage. Does anyone shares this exprerience with me or does anyone knows an answer? For the...
8
4324
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
3150
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. The script just dies in the file- read operation, with no symptom. I tried unsuccessfully to get the Debugger to run the HTML file.
6
1940
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 timer to load the php page via ajax every 15 seconds which checks the "pinguser" table to see if the user has any chat requests, then echoes them to...
7
2084
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
7664
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7885
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8106
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7638
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
6250
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
3642
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
2082
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1198
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
923
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.