/    Sign up×
Community /Pin to ProfileBookmark

[RESOLVED] Guarantee AJAX load done?

Is there an accepted way to guarantee that an ajax function has
finished loading information from a file. I appears that the script continues
processing without having the information available to process. 😮

The sequence in my script starting at the <body onload=> is:
1. Load contents of an external text file into a <textarea>
2. Form an array of the lines of the file loaded (splitting at the ‘n’)
3. Display the contents of the processed array using a <select>.

I used the idea (without the ajax function) from another forum post.
See: [url]http://www.webdeveloper.com/forum/showthread.php?t=182985[/url]

Here is my attempt.

Note, if you try it, that the select box will work correctly after you click the top button
(process data) to re-process the textarea information, but not before.
But it just repeats what part of the onload did, so there should be no difference. ???

[code=php]
<html>
<head>
<title>SKU database</title>

<script type=”text/javascript”>
var SKUhash = {}; // pseudo-hash

// From: http://www.w3schools.com/ajax/ajax_serverscript.asp

function ajaxFunction(filename, ids) {
var xmlHttp;
try { // Firefox, Opera 8.0+, Safari
xmlHttp=new XMLHttpRequest();
}
catch (e) { // Internet Explorer
try { xmlHttp=new ActiveXObject(“Msxml2.XMLHTTP”); }
catch (e) {
try { xmlHttp=new ActiveXObject(“Microsoft.XMLHTTP”); }
catch (e) {
alert(“Your browser does not support AJAX!”);
return false;
}
}
}
xmlHttp.onreadystatechange=function() {
if (xmlHttp.readyState==4) {
document.getElementById(ids).value=xmlHttp.responseText;
}
}
xmlHttp.open(“GET”,filename,true);
xmlHttp.send(null);
}
/* next function made no difference if a delay was inserted before processing
function pausecomp(millisec) { // attempt to wait before more processing
var date = new Date();
var curDate = null;
do { curDate = new Date(); } while (curDate-date < millisec);
}
*/
function LoadData(fname) {
var SKUs = new Array();
ajaxFunction(‘SKU.txt’,’Info’);
}

function ReadData() {
// pausecomp(1000);

var RecStr = document.getElementById(‘Info’).value;
SKUs = RecStr.split(“n”);
str = SKUs.shift(); // drop comment record

for (i=0; i<SKUs.length-1; i++) {
str = SKUs[i];
var A=str.split(‘,’);
SKUhash[A[0]]=A.join(‘,’); // form pseudo-hash record with same contents as string input
}
// alert(SKUs.join(‘n’));
}

</script>

<script type=”text/javascript” language=”javascript”>
// From: http://www.webdeveloper.com/forum/showthread.php?t=182985

/*
// (and modified format in ScriptSKUs.txt)
# Contents of SKUselect.js === (posting,) sku,title,industry,nsn,industry,msrp
BLK-30-7,M.O.D. Keating Hornet Automatic Folding Knife,TLE,&nbsp;,0,249.99
BLK-30-8,M.O.D. Keating Hornet Serrated Edge Automatic Folding Knife; Black Finish,&nbsp;,&nbsp;,0,249.99
*/

function showValue(box, outputID) {
if (box == ”) { return; }
var dataString = SKUhash[box];
document.getElementById(‘itembuybox2’).innerHTML=dataString;
}

</script>
</head>
<body onload=”LoadData(‘SKU.txt’);ReadData()”>

<form name=”GetItem” method=”post” action=”#”>
Item:
<select name=”Items” id=”ItemSelect” class=”select” onchange=”showValue(this.value,’itembuybox2′);”>

<option value=”” selected=”selected”>Choose an item</option>
<option value=”BLK-30-7″>M.O.D. Keating Hornet Automatic Folding Knife</option>
<option value=”BLK-30-8″>M.O.D. Keating Hornet Serrated Edge Automatic</option>
</select>
</form>

<div id=”itembuybox2″>
</div>

<p>

<button onClick=”ReadData()”>Process textarea</button><br />
<textarea id=”Info” rows=”10″ cols=”120″></textarea>
<button onClick=”LoadData(‘SKU.txt’)”>Reload SKU.txt</button><br />
</body>
</html>
[/code]

External ‘SKU.txt’ file looks like this:

[code]
# Contents of SKUselect.js === sku,title,industry,nsn,industry,msrp
BLK-30-7,M.O.D. Keating Hornet Automatic Folding Knife,TLE,&nbsp;,0,249.99
BLK-30-8,M.O.D. Keating Hornet Serrated Edge Automatic Folding Knife; Black Finish,&nbsp;,&nbsp;,0,249.99
[/code]

Thanks in advance for any thought about my problem.
?

to post a comment
JavaScript

4 Comments(s)

Copy linkTweet thisAlerts:
@JMRKERauthorJun 05.2008 — Another question,

In the working code of post #1

would it do any good to move the LoadData-ReadData functions

via a delayed load into he head section at the end of the HTML part?

In other words, is the failure to read the data a function of

a. the sequence of the header then html sections

or,

b. should it be header - html - add ajax logic to header - then call functions?

Again, code works fine ... I just have to click on the first button

before the select choices return the information properly.

Is there anything I can do to fix this? ?
Copy linkTweet thisAlerts:
@Declan1991Jun 05.2008 — If you use SJAX, it will pause all JavaScript until completed.
Copy linkTweet thisAlerts:
@JMRKERauthorJun 05.2008 — Thank you @Declan1991.

I've not heard of SJAX before.


Do you have a favorite site to view on the topic or just 'google' it?

I appreciate the comment.
Copy linkTweet thisAlerts:
@JMRKERauthorJun 05.2008 — Perfect. It turned out to be a one line change in the code.
[code=php]
// xmlHttp.open("GET",filename,true); // AJAX solution
xmlHttp.open("GET",filename,false); // SJAX solution
// SJAX solution from: http://www.power-coder.net/2007/01/synchronous-ajax-or-sjax-for-short/
[/code]


Thanks @Declan1991 for the information about SJAX ... who knew!
×

Success!

Help @JMRKER 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 4.29,
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: @Yussuf4331,
tipped: article
amount: 1000 SATS,

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

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...