What i'm trying to do is implement a simple tagging feature for articles on my site. A user types in a tag, its get recording in the db and gets outputted to the screen with all the other tags. What happens in IE is that first couple of times it works, but then it just stops working.
I linked the javascript function to a link ("<a href="javascript:function()">...</a>") but when i click on the link it all of a sudden doesnt work, it does nothing. This is after it working for the previous 2 or three times.
Also, when i type a new tag (tag1), click the link, it updates the list of tags. I type another new tag (tag2), click the link, it updates the list of tags. Now when type in tag1 again, click the link, tag2 dissappears from the list?!!?!
is there something i'm doing wrong? Do i need to do something with the activeXobject? I ran the code with MS script debugger, and it is returning an object on those "do nothing calls", however it is not performing the xmlhttp.open() function. I verified this by checking packets coming in and out. There were no calls to the serverside script when call the javascript function.
Here is my code:
Code: Select all
<script language="JavaScript" type="text/javascript">
//XMLhttp variable will hold the XMLHttpRequest object
function getHTTPObject() {
var xmlhttp;
xmlhttp = false;
// If the user is using Mozilla/Firefox/Safari/etc
if (window.XMLHttpRequest) {
//Intiate the object
xmlhttp = new XMLHttpRequest();
//Set the mime type
//xmlhttp.overrideMimeType('text/html');
}
// If the user is using IE
else if (window.ActiveXObject) {
//Intiate the object
xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
}
return xmlhttp;
}
function preSearch() {
var xmlhttp = getHTTPObject(); // We create the HTTP Object
//Put the form data into a variable
var theQuery = document.getElementById('tag_id').value;
var job_id = document.getElementById('j').value;
//If the form data is *not* blank, query the DB and return the results
if(theQuery !== "") {
var url = 'ajax.php?tag=' + theQuery + '&j=' + job_id;
//document.write(url);
//Open the URL above "asynchronously" (that's what the "true" is for) using the GET method
if(xmlhttp) {
xmlhttp.open('GET', url, true);
//Check that the PHP script has finished sending us the result
xmlhttp.onreadystatechange = function() {
//document.getElementById('tag_results').innerHTML = xmlhttp.readyState;
if((xmlhttp.readyState == 4)&&(xmlhttp.status == 200)) {
//Replace the content of the "result" DIV with the result returned by the PHP script
document.getElementById('tag_results').innerHTML = xmlhttp.responseText + ' ';
} else {
//If the PHP script fails to send a response, or sends back an error, display a simple user-friendly notification
document.getElementById('tag_results').innerHTML = ' Tagging... ';
}
};
xmlhttp.send(null);
}
else { document.getElementById('tag_results').innerHTML = 'xmlhttp is false'; }
}
}
function preSearchBool(e) {
var pK = document.all? window.event.keyCode:e.which;
return pK != 13;
}
document.onkeypress = preSearchBool;
</script>