javascript 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=="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 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.

Answers


use 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?


Need Your Help

Accessing LinkedServer properties via SQL Server SMO API

sql-server sql-server-2005 powershell smo

I have a powershell script to iterate through a list of LinkedServer objects in a SQL Server with some code like this:

How to hide a custom right click Menu

jquery html css twitter-bootstrap-3

I found the source of the page (How to add a custom right-click menu to a webpage?) about "How to add a custom right-click menu to a webpage"