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=="plete")
{
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 being called. get_employee.php works fine when I call it by itself, so that is not the problem. Is there anything wrong in my code that would prevent it from being called? I cannot test with any firefox extensions, I do not have access, so please don't give that as an answer.
edit: the problem is the javascript is not being called at all. I fixed the question mark problem, but even just a simple javascript with an alert is not being 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=="plete")
{
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 being called. get_employee.php works fine when I call it by itself, so that is not the problem. Is there anything wrong in my code that would prevent it from being called? I cannot test with any firefox extensions, I do not have access, so please don't give that as an answer.
edit: the problem is the javascript is not being called at all. I fixed the question mark problem, but even just a simple javascript with an alert is not being called.
Share Improve this question edited Nov 20, 2008 at 13:03 user1253538 asked Nov 20, 2008 at 12:43 Joshxtothe4Joshxtothe4 4,22110 gold badges56 silver badges86 bronze badges 3- ken: Why did you feel the need to edit? – user1253538 Commented Nov 20, 2008 at 12:52
- @Johsxtothe4 - consistency. JavaScript should be JavaScript – Ken Commented Nov 20, 2008 at 13:18
- I don't see what causes FetchCommplete to be called. Shouldn't there be onload in there somewhere? Also, check your server logs to see if ajaxlib.js and get_employee.php are ever getting fetched. – Paul Tomblin Commented Nov 20, 2008 at 13:22
10 Answers
Reset to default 1use a javascript debugging tool like firebug, this will make your life simpler.
you had a syntax error in your code that made the error "GetEmployee is not defined"
it was a 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;
}
var url="get_employee.php?"
Needs the "?".
It's better to use this markup to include your scripts:
<script type="text/javascript" src="ajaxlib.js"></script>
Change this
var url="get_employee.php"
url=url+"cmd=GetEmployee"
url=url+"&sid="+Math.random()
to this:
var url="get_employee.php?cmd=GetEmployee&sid="+Math.random();
You were missing the "?" and there's no need for all of the concatenation (but I guess that's just personal style).
Also, if you actually have the "<The result will be fetched here>" in your html, you should remove it.
I am a bit confused about putting the <script>
tag into the no man's land between head and body. Does this have some special meaning?
Shouldn't there be a question mark or an ampersand between the getmand.php
and the cmd=
parts?
I don't suppose its something silly like a missinq question mark in the url
var url="get_employee.php"
url=url+"cmd=GetEmployee"
url=url+"&sid="+Math.random()
I would have expected to see "?cmd=GetEmployee"
Make sure you aren't misplacing or naming a file. You can change OnClick to all lowercase too.
var url="get_employee.php" + "?"
(answering the ment)
What error is reported? You still have to attach your FetchComplete function to xmlHttp's "onreadystatechange" property, but it shouldn't be an error not to do it.
Make sure that ajaxlib.js is really loaded, and that it is the file you mean. Put some alerts and see if they pop up.
If you can't use a proper debugger, you can add alert statements all over the place to see if anything is happening at all (yes, it's a bad solution, but anytime you don't use a good tool you have a bad solution).
Also, make sure the OnClick() returns false, to prevent the browser from reloading the page.
<a href="#" OnClick="GetEmployee()">link</a>
bees
<a href="#" OnClick="GetEmployee(); return false;">link</a>
You can use alert(url)
to check the exact url being sent.