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.

+1


source to share


10 replies


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;
}

      

+1


source


var url="get_employee.php?"

      

Required "?"



Better to use this markup to include your scripts:

<script type="text/javascript" src="ajaxlib.js"></script>

      

+1


source


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.

+1


source


I'm a little confused about putting the tag <script>

in the ground without a person between my head and body. Does it have any special meaning?

+1


source


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.

0


source


Shouldn't there be a question mark or ampersand between getcommand.php

and cmd=

?

0


source


I don't think this is something stupid like missinq question mark in url

var url="get_employee.php"
url=url+"cmd=GetEmployee"
url=url+"&sid="+Math.random()

      

I would expect to see "?cmd=GetEmployee"

0


source


You can use alert(url)

to check the exact URL submitted.

0


source


Make sure you don't confuse or name the file. You can also change OnClick to all lowercase letters.

0


source


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>

0


source







All Articles