P
pbd22
Hi.
My below code works in IE but not in Firefox.
I think this is a aync vs. sync issue but I need some
clarification (and advice) here.
I have a function with the following code in it:
1. var myJSONtxt = GetChildCategories(parentID);
2. var json = JSON.parse(myJSONtxt);
In IE, the script waits for GetChildCategories to return
the JSON string before it executes line 2.
In Firefox, line 1 gets evaluated and moves on to line 2 before
the xmlhttp request object can return its value to myJSONtxt.
As a result "null" gets parsed and json throws all sorts of errors.
Can somebody please give me a "code example" of how I should
do this so it works in both IE and Firefox? I'd prefer non-library
based solutions. Below is the called function:
function GetChildCategories(parentID)
{
var xmlhttp=false;
var returnObj;
try {
//Show page is loading
document.getElementById('tabs-1').innerHTML = 'Loading
page Please wait... <img src="images/loading.gif">';
/*@cc_on @*/
/*@if (@_jscript_version >= 5)
// JScript gives us Conditional compilation, we can
cope with old IE versions.
// and security blocked creation of the objects.
try {
xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
} catch (E) {
xmlhttp = false;
}
}
@end @*/
if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
try {
xmlhttp = new XMLHttpRequest();
} catch (e) {
xmlhttp=false;
}
}
if (!xmlhttp && window.createRequest) {
try {
xmlhttp = window.createRequest();
} catch (e) {
xmlhttp=false;
}
}
var url = "trade.svc/getchildren/" + id;
xmlhttp.open("GET", url, false);
xmlhttp.onreadystatechange=function() {
if (xmlhttp.readyState==4) {
if(xmlhttp.readyState == 4)
{
returnObj = xmlhttp.responseText;
}
}
}
xmlhttp.send(null)
} catch (e) { alert("E: " + e.message);}
return returnObj;
}
My below code works in IE but not in Firefox.
I think this is a aync vs. sync issue but I need some
clarification (and advice) here.
I have a function with the following code in it:
1. var myJSONtxt = GetChildCategories(parentID);
2. var json = JSON.parse(myJSONtxt);
In IE, the script waits for GetChildCategories to return
the JSON string before it executes line 2.
In Firefox, line 1 gets evaluated and moves on to line 2 before
the xmlhttp request object can return its value to myJSONtxt.
As a result "null" gets parsed and json throws all sorts of errors.
Can somebody please give me a "code example" of how I should
do this so it works in both IE and Firefox? I'd prefer non-library
based solutions. Below is the called function:
function GetChildCategories(parentID)
{
var xmlhttp=false;
var returnObj;
try {
//Show page is loading
document.getElementById('tabs-1').innerHTML = 'Loading
page Please wait... <img src="images/loading.gif">';
/*@cc_on @*/
/*@if (@_jscript_version >= 5)
// JScript gives us Conditional compilation, we can
cope with old IE versions.
// and security blocked creation of the objects.
try {
xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
} catch (E) {
xmlhttp = false;
}
}
@end @*/
if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
try {
xmlhttp = new XMLHttpRequest();
} catch (e) {
xmlhttp=false;
}
}
if (!xmlhttp && window.createRequest) {
try {
xmlhttp = window.createRequest();
} catch (e) {
xmlhttp=false;
}
}
var url = "trade.svc/getchildren/" + id;
xmlhttp.open("GET", url, false);
xmlhttp.onreadystatechange=function() {
if (xmlhttp.readyState==4) {
if(xmlhttp.readyState == 4)
{
returnObj = xmlhttp.responseText;
}
}
}
xmlhttp.send(null)
} catch (e) { alert("E: " + e.message);}
return returnObj;
}