Javascript is not called
I am using this HTML
<html>
<head>
<Title>EBAY Search</title>
</head>
<script language="JavaScript" src="ajaxlib.js"></script>
<body>
Click here <a href="#" OnClick="GetEmployee()">link</a> to show content
<div id="Result"><The result will be fetched here></div>
</body>
</html>
With this Javascript
var xmlHttp
function GetEmployee()
{
xmlHttp=GetXmlHttpObject()
if(xmlHttp==null)
{
alert("Your browser is not supported")
}
var url="get_employee.php"
url=url+"cmd=GetEmployee"
url=url+"&sid="+Math.random()
xmlHttp.open("GET",url,true)
xmlHttp.send(null)
}
function FetchComplete()
{
if(xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
{
document.getElementById("Result").innerHTML=xmlHttp.responseText
}
if(xmlHttp.readyState==1 || xmlHttp.readyState=="loading")
{
document.getElementById("Result").innerHTML="loading"
}
}
function GetXmlHttpObject()
{
var xmlHttp=null;
try
{
xmlHttp=new XMLHttpRequest();
}
catch (e)
{
try
{
xmlHttp =new ActiveXObject("Microsoft.XMLHTTP");
}
}
return xmlHttp;
}
However, it is not called. get_employee.php works fine when I call it on my own, so this is not a problem. Is there something wrong in my code that would prevent it from being called? I can't test with any firefox extensions, I don't have access, so please don't give this as an answer.
edit: the problem is the javascript is not being called at all. I fixed the question mark issue, but even simple JavaScript with a warning is not being called.
source to share
use a javascript debugging tool like firebug, it will make your life easier.
you had a syntax error in your code that made the error "GetEmployee is not defined"
it was the missing catch after the last try in GetXmlHttpObject (). this is the same function after adding the missing catch.
function GetXmlHttpObject()
{
var xmlHttp=null;
try
{
xmlHttp=new XMLHttpRequest();
}catch (e)
{
try
{
xmlHttp =new ActiveXObject("Microsoft.XMLHTTP");
}
catch (e) {}
}
return xmlHttp;
}
source to share
Change this
var url="get_employee.php"
url=url+"cmd=GetEmployee"
url=url+"&sid="+Math.random()
:
var url="get_employee.php?cmd=GetEmployee&sid="+Math.random();
Did you miss the "?" and there is no need for all concatenations (but I assume this is just personal style).
Also, if you actually have "<Result will be retrieved here>" in your html, you should remove it.
source to share
var url = "get_employee.php" + "?"
(reply to comment)
What error is being reported? You will still have to attach your FetchComplete function to the xreadHateHreadHateHostHeadystatechange property, but it shouldn't be a mistake not to.
Make sure ajaxlib.js is actually loaded and that this is the file you mean. Post some warnings and see if they appear.
source to share
If you can't use a proper debugger, you can add warning messages all over the place to see if something is going on at all (yes, it's a bad solution, but anytime you're not using a good tool you have a bad solution).
Also, make sure OnClick () returns false to prevent the browser from reloading the page.
<a href="#" OnClick="GetEmployee()">link</a>
becomes
<a href="#" OnClick="GetEmployee(); return false;">link</a>
source to share