/    Sign up×
Community /Pin to ProfileBookmark

I am having a problem using the AHAH method to load data into a div tag…

I am trying to load the output from fort.php into

[code=html]<div id=”fdiv”>Loading Fortune</div>[/code]

And yet, it shows a getElementById(target) error.
Here is the Javascript I am using:

[code=html]function jah(url,target) {
// native XMLHttpRequest object
document.getElementById(target).innerHTML = ‘sending…’;
if (window.XMLHttpRequest) {
req = new XMLHttpRequest();
req.onreadystatechange = function() {jahDone(target);};
req.open(“GET”, url, true);
req.send(null);
// IE/Windows ActiveX version
} else if (window.ActiveXObject) {
req = new ActiveXObject(“Microsoft.XMLHTTP”);
if (req) {
req.onreadystatechange = function() {jahDone(target);};
req.open(“GET”, url, true);
req.send();
}
}
setTimeout(‘jah(“fort.php?head=1”, “fdiv”)’, 15000);
}

function jahDone(target) {
// only if req is “loaded”
if (req.readyState == 4) {
// only if “OK”
if (req.status == 200) {
results = req.responseText;
document.getElementById(target).innerHTML = results;
} else {
document.getElementById(target).innerHTML=”jah error:n” +
req.statusText;
}
}
}
jah(“fort.php?head=1”, “fdiv”);[/code]

If anyone could help me, it would be FANTASTIC

to post a comment
JavaScript

4 Comments(s)

Copy linkTweet thisAlerts:
@cridleySep 22.2006 — by the time it calls the statechange function the target variable has been destroyed as it belongs to the first function only. You'll need it to be global to the two functions, try:

[code=html]
var gTarget;
function jah(url,target) {
// native XMLHttpRequest object
document.getElementById(target).innerHTML = 'sending...';
if (window.XMLHttpRequest) {
gTarget = target
req = new XMLHttpRequest();
req.onreadystatechange = jahDone
req.open("GET", url, true);
req.send(null);
// IE/Windows ActiveX version
} else if (window.ActiveXObject) {
req = new ActiveXObject("Microsoft.XMLHTTP");
if (req) {
gTarget = target
req.onreadystatechange = jahDone
req.open("GET", url, true);
req.send();
}
}
setTimeout('jah("fort.php?head=1", "fdiv")', 15000);
}

function jahDone() {
// only if req is "loaded"
if (req.readyState == 4) {
// only if "OK"
if (req.status == 200) {
results = req.responseText;
document.getElementById(gTarget).innerHTML = results;
} else {
document.getElementById(gTarget).innerHTML="jah error:n" +
req.statusText;
}
}
}
jah("fort.php?head=1", "fdiv");
[/code]
Copy linkTweet thisAlerts:
@cridleySep 22.2006 — setTimeout('jah("fort.php?head=1", "fdiv")', 15000);

should be

setTimeout('jah("fort.php?head=1", gTarget)', 15000);

otherwise what's the point of passing in a target if it's fixed?
Copy linkTweet thisAlerts:
@necromantulaauthorSep 23.2006 — Alright, my new code is:
[code=html]var gTarget = "fdiv" ;
function jah(url) {
// native XMLHttpRequest object
document.getElementById(gTarget).innerHTML = 'sending...';
if (window.XMLHttpRequest) {
req = new XMLHttpRequest();
req.onreadystatechange = jahDone
req.open("GET", url, true);
req.send(null);
// IE/Windows ActiveX version
} else if (window.ActiveXObject) {
req = new ActiveXObject("Microsoft.XMLHTTP");
if (req) {
req.onreadystatechange = jahDone
req.open("GET", url, true);
req.send();
}
}
setTimeout('jah("fort.php?head=1")', 15000);
}

function jahDone() {
// only if req is "loaded"
if (req.readyState == 4) {
// only if "OK"
if (req.status == 200) {
results = req.responseText;
document.getElementById(gTarget).innerHTML = results;
} else {
document.getElementById(gTarget).innerHTML="jah error:n" +
req.statusText;
}
}
}
jah("fort.php?head=1");[/code]


I am testing this in Firefox and IE, in IE the error is:
[code=html]Line: 5
Char 4
Error: 'document.getElementById(...)' is null or not an object
Code: 0
URL: http://dochaos.com/[/code]


In Firefox the error is: [code=html]Error: document.getElementById(gTarget) has no properties
Source File: http://dochaos.com/x/load.js
Line: 4[/code]

(Retrieved from the Web Developer Toolbar's Javascript Console)

Any ideas?
Copy linkTweet thisAlerts:
@A1ien51Sep 23.2006 — Look into prototype.js and its Ajax.Updater

Eric
×

Success!

Help @necromantula spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...