/    Sign up×
Community /Pin to ProfileBookmark

The first Ajax function call always skipped

Problem:
Trying to call the same Ajax function twice on initial load of the page, but only the second one executed.

I try to load mainpage, and inside mainpage I try to use ajax to load content1.asp to div1 and content2.asp to div2.

Test scenario (based on the [B]Situation[/B] below):
– Page loaded, 1st ajax function skipped, 2nd ajax function loaded
– Remarked 1st ajax function, then 2nd ajax function loaded
– Remarked 2nd ajax function, then 1st ajax function loaded
– Moved the 2nd ajax function to above 1st ajax function, the 2nd ajax function is skipped.
Conclution:
– the 1st ajax function always skipped. But Why? How to ensure both ajax is called upon loading of the page?

What is the problem?

[B]Situation:[/B]
[B]mainpage.asp[/B]
<div id=”div1″></div>
<div id=”div2″></div>
<script language=”JavaScript1.2″>
ajax(‘div1’, ‘GET’, ‘page1.asp’);
ajax(‘div2’, ‘GET’, ‘page2.asp’);
</script>

[B]ajax.js –[/B]
function ajax(strDivID, strMethod, strURL)
{
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject(“Microsoft.XMLHTTP”);
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById(strDivID).innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open(strMethod,strURL,true);
if (strMethod=”POST”)
{
xmlhttp.setRequestHeader(“Content-type”,”application/x-www-form-urlencoded”);
}
xmlhttp.send();
}

to post a comment
JavaScript

1 Comments(s)

Copy linkTweet thisAlerts:
@Sterling_IsfineOct 09.2010 — Don't use a global request object as it gets overwritten.

Here's a slight improvement:[CODE]function ajax(strDivID, strMethod, strURL)
{
[B]var xmlhttp = null;[/B]
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
if( window.ActiveXObject )
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
if( xmlhttp )
{
xmlhttp.onreadystatechange=function()
{
if ([B]this[/B].readyState==4 && [B]this[/B].status==200)
{
document.getElementById(strDivID).innerHTML = [B]this[/B].responseText;
}
}

xmlhttp.open(strMethod,strURL,true);
if (strMethod[COLOR="Red"]=[/COLOR]="POST")
{
xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
}
xmlhttp.send();
}
}
[/CODE]
×

Success!

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