I've got this little AJAX applet I made. For some reason IE gives me a 'type mismatch error' if I put parentheses after http.onReadyStateChange = handleResponse (making it http.onReadyStateChange = handleResponse()). Any ideas on how to get this to work both ways without having to check for browser? Code is ...
| | #1 | ||||
| Member libertarian New York City, New York ![]()
| Nerd Crew :: weird js issue with ff/ie I've got this little AJAX applet I made. For some reason IE gives me a 'type mismatch error' if I put parentheses after http.onReadyStateChange = handleResponse (making it http.onReadyStateChange = handleResponse()). Any ideas on how to get this to work both ways without having to check for browser? Code is below. I'm aware that it's not really asynchronous with the 'false' parameter in the .open method but it won't work at all in FF without it there. I really just wanted to make a quick thing that didn't reload the page. Code: <script language="Javascript">
function createRequestObject() {
var reqObj;
if (window.ActiveXObject) {
reqObj = new ActiveXObject("Microsoft.XMLHTTP");
}//end if
else if (window.XMLHttpRequest){
reqObj = new XMLHttpRequest();
}//end else if
return reqObj;
} //end function
var http = createRequestObject();
function sndReq(action) {
var str = "catName="+action.catBox.value;
var file = "apDBSrch.php";
http.open("POST", file, false);
http.setRequestHeader("Content-Type","application/x-www-form-urlencoded; charset=UTF-8");
http.send(str);
http.onreadystatechange = handleResponse();
} // end function
function handleResponse() {
if(http.readyState == 4){
var response = http.responseText;
var update = new Array();
if(response.indexOf('|') != -1) {
update = response.split('|');
document.getElementById(update[0]).innerHTML = update[1];
} //end if
} //end if
} //end function
</script> | ||||
| Register to Reply to This Post |
| | #2 | ||||
| For those about to rock... libertarian Atlanta, GA ![]() ![]()
| I've never done any AJAX (though I have done a bit of java), so sorry if my questions are stupid. What does handleResponse return? I mean, I don't see it giving you anything to actually set something else equal to. Maybe it's passing by reference and I just don't see it though | ||||
| Register to Reply to This Post |
| | #3 | ||||
| Member libertarian New York City, New York ![]()
| Yeah it doesn't actually return anything. It parses the return string and does the innerHTML change so the text shows up on the page. I don't understand exactly how it works tbh. If it were getting a value it would get null, I assume. | ||||
| Register to Reply to This Post |
| | #4 | ||||
| For those about to rock... libertarian Atlanta, GA ![]() ![]()
| I'm just saying, if it passes by reference, I don't see it, and it has to pass by SOMETHING for you to set something else equal to it. So, what you're saying is, with the parens, it works find in ff, but not in ie? without, does it work in ie? | ||||
| Register to Reply to This Post |
| | #5 | ||||
| Member libertarian New York City, New York ![]()
| Without it works in IE, with it gives me a 'type mismatch' error but still works. | ||||
| Register to Reply to This Post |
| | #6 | ||||
| For those about to rock... libertarian Atlanta, GA ![]() ![]()
| can you try casting the handleResponse to http? | ||||
| Register to Reply to This Post |
| | #7 | ||||
| Member libertarian New York City, New York ![]()
| You can only typecast to number/string iirc. | ||||
| Register to Reply to This Post |
| | #8 | ||||
| For those about to rock... libertarian Atlanta, GA ![]() ![]()
| You can't cast to an object? I mean, in normal OOP languages, you make casting definitions within the object functions. ![]() Can you call that line differently? Like I said, I've never programmed AJAX, so I don't even know why those parens are needed. | ||||
| Register to Reply to This Post |
| | #9 | ||||
| Member libertarian New York City, New York ![]()
| Well, you don't need the parens in IE but you do for FF. I'm not sure why. | ||||
| Register to Reply to This Post |
| | #10 | ||||
| For those about to rock... libertarian Atlanta, GA ![]() ![]()
| I just asked one of my students (he's a programming genius). He says that since you're battling the ineffectiveness of javascript (not java), and you can't cast it void and recast it the correct type, you'll just have to do an ie hack (that is, make an if statment right at that line that is something like if browser = ie, do it one way, if browser = ff, do it another). Sucks, but should be an easy fix. | ||||
| Register to Reply to This Post |
| | #11 | ||||
| Member libertarian New York City, New York ![]()
| Yeah I figured I would have to do something like that. I wanted to see if there was a different way of fixing that up. Bah. Thanks for puzzling through it with me | ||||
| Register to Reply to This Post |
| | #12 | ||||
| For those about to rock... libertarian Atlanta, GA ![]() ![]()
| |||||
| Register to Reply to This Post |
| | #13 | ||||
| Banned Conservative Government is another way to say Better Than You ![]()
| | ||||
| Register to Reply to This Post |
| vBulletin 3.7.4 -- Copyright ©2000 - 2008, Jelsoft Enterprises Ltd. | Custom Artwork and Theme (TM) 2006, Liberty Lounge |