/    Sign up×
Community /Pin to ProfileBookmark

Crazy Crazy Crazy xmlhttp.responseXML.getElementsByTagName enigma

Does anyone know why the javascript below will hang unless there is an alert placed right before it? Does the shift in focus clear some problem??? ARRRGGHHHH??? driving me crazy please help.

this hangs:
var x=xmlhttp.responseXML.documentElement.getElementsByTagName(“ITM”);

this doesn’t:
alert(“”);
var x=xmlhttp.responseXML.documentElement.getElementsByTagName(“ITM”);

when I click the alert the rest of the code runs no problem, but without it just white space with no error

Thanks in advance to anyone who can help me gain my sanity.

T

to post a comment
JavaScript

6 Comments(s)

Copy linkTweet thisAlerts:
@jamOct 25.2009 — Have you tried void()ing before it? just so that the problem is less noticable? Sorry, i'm unsure of the actual problem.

Also it could be an idea to try break down the statement into a few, so that you can pinpoint which area is actually causing it.

Cheers,

Jamey
Copy linkTweet thisAlerts:
@toicontienOct 25.2009 — Is your AJAX request being sent synchronously or asynchronously. By default, AJAX requests are sent asynchronously, which means the very next line of JavaScript after you send the request might execute before the request gets back to the browser. The alert() causes the browser user interface, and JavaScript execution, to freeze until the user does something, which is probably giving the AJAX response enough time to get back to the browser.
Copy linkTweet thisAlerts:
@tschwarzauthorOct 25.2009 — Thanks jam sorry I was unclear. I posted more of the code to try and explain. What I am doing is generating a page of patterns and pulling the url and name of the patterns from an xml page the owner can update himself.

I am loading the data in the loadXMLDOC function and building the page in the creatediv function. The data loads without error, but when I try and access the results the page hangs (no error). If I place an alert right before the line to retrieve the data like this:

alert("");
var a=0;
var x=xmlhttp.responseXML.documentElement.getElementsByTagName("ITM");


then once the alert is cleared it goes through no problem. Any thoughts?

Thanks

T



[CODE]<script type="text/javascript">

var xmlhttp;

function loadXMLDoc(url)
{

xmlhttp=null;
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE5, IE6
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=onResponse;
xmlhttp.open("GET",url,true);
xmlhttp.send(null);

creatediv();
}

function onResponse()
{
// alert("rs"+xmlhttp.readyState);
// alert("st"+xmlhttp.status);
if(xmlhttp.readyState!=4) return;

if(xmlhttp.status!=200)
{
alert("Problem retrieving XML data");
return;
}

}

function creatediv() {
alert("");
var a=0;
var x=xmlhttp.responseXML.documentElement.getElementsByTagName("ITM");
var wrapper = document.createElement('div');
wrapper.setAttribute('id', "wrapper");
document.body.appendChild(wrapper);[/CODE]
Copy linkTweet thisAlerts:
@tschwarzauthorOct 25.2009 — thanks for the input toicontien , If this is the problem then what is my solution? Do I put a delay between the load and the create functions? Also, I have recently found out that chrome/safari are not cooperating at all. Even with the alert.

T
Copy linkTweet thisAlerts:
@toicontienOct 27.2009 — You are sending an asynchronous request:
[CODE]xmlhttp.open("GET",url,[B]true[/B]);[/CODE]

You need to utilize the onreadystatechange event handler:

[CODE]function onResponse() {
if ( this.readyState == 4 && this.status == 200 ) {
creatediv();
}
}[/CODE]
Copy linkTweet thisAlerts:
@tschwarzauthorOct 27.2009 — Thanks much toicontien, that was it. I wound up changing the onResponse function to match your advice and viola works across browsers without the alert/delay...

[CODE]function onResponse()
{

if(xmlhttp.readyState==4 && xmlhttp.status==200)
{
creatediv();
}
else
{
return;
}[/CODE]
×

Success!

Help @tschwarz 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.19,
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,
)...