Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

JavaScript Forum


You are currently viewing our JavaScript Forum as a guest. Please register to participate.
Login



Reply
Old 02-23-2007, 01:56 PM Need some AJAX help
TyR
TyR's Avatar
Super Talker

Posts: 139
Location: Frei - Norway
Trades: 0
Hi, I'm working on this little script:

ajax.js:
Code:
var ajax_req = getXmlHttpRequestObject();

function getXmlHttpRequestObject() {
	if (window.XMLHttpRequest) {
		return new XMLHttpRequest();
	} else if(window.ActiveXObject) {
		return new ActiveXObject("Microsoft.XMLHTTP");
	} else {
		alert("XmlHttpRequest not supported");
	}
}

function ajax_load(x, y) {
	if (ajax_req.readyState == 4 || ajax_req.readyState == 0) {
		ajax_req.open("GET", x, true);
		ajax_req.onreadystatechange = function () { y };
		ajax_req.send(null);
	}
}

function ajax_output(x) {
	if (ajax_req.readyState == 4) {
		document.getElementById(x).innerHTML = ajax_req.responseText;
	}
}
HTML:
HTML Code:
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
        <script src="engine/ajax.js" type="text/javascript"></script>
    </head>
    
    <body>

        <button id="moop">LOAD</button>
        <span id="result"></span>

        <script>
            document.getElementById("moop").onclick = function () { ajax_load('forsiden.php', ajax_output('result')); }
        </script>

    </body>
</html>
And it kinda works, but you have to press the button two times for the text to appear, and I need some help to understand why, and how I can fix this.

Could anyone point me in the right direction?

Thanks
__________________

Please login or register to view this content. Registration is FREE


Please login or register to view this content. Registration is FREE


Please login or register to view this content. Registration is FREE
TyR is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 02-24-2007, 05:52 PM Re: Need some AJAX help
Novice Talker

Posts: 7
Trades: 0
I don't know why exactly you're having that problem, but I would put the button into a form, set the onclick attribute directly in that tag, and take a look at the Prototype library.
__________________
Please help build a
Please login or register to view this content. Registration is FREE
:
Please login or register to view this content. Registration is FREE
,
Please login or register to view this content. Registration is FREE
veridicus is offline
Reply With Quote
View Public Profile Visit veridicus's homepage!
 
Old 03-01-2007, 03:09 AM Re: Need some AJAX help
Junior Talker

Posts: 1
Name: Bill Schmidt
Trades: 0
It looks like this might be the problem:
Code:
if (ajax_req.readyState == 4 || ajax_req.readyState == 0) {
try this:
Code:
if (ajax_req.readyState == 4 && ajax_req.status == 200) {
you'll have to move the ajax_req.open to before that if block

Last edited by psychopsi; 03-01-2007 at 03:16 AM..
psychopsi is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to Need some AJAX help
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.23737 seconds with 12 queries