Question:

Problem with AJAX javascript?

by  |  earlier

0 LIKES UnLike

Once function AJAX() is called it cannot be called again.

Code:

<script type="text/javascript">

<!--

var xmlHttp;

try

{ // Firefox, Opera 8.0+, Safari

xmlHttp=new XMLHttpRequest();

}

catch (e)

{ // Internet Explorer

try

{

xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");

}

catch (e)

{

try

{

xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");

}

catch (e)

{

alert("Your browser does not support AJAX!");

}

}

}

function AJAX(Ref,Spec,Elem)

{

xmlHttp.onreadystatechange=function()

{

if(xmlHttp.readyState==4)

{

var oldElem = document.getElementById(Elem);

oldElem.innerHTML = xmlHttp.responseText;

}

}

ReqStr = "AJAX.php?Ref=" + Ref + "&Spec=" + Spec + "&Elem=" + Elem;

xmlHttp.open("Get",ReqStr,true);

xmlHttp.send(null);

}

//-->

</script>

http://trollnest.com/bragflags/default.php

To see behavior, roll over category menu item, works once then never again.

It is stuck in request loop I believe.

 Tags:

   Report

2 ANSWERS


  1. If it were stuck in a loop, the page would appear to &quot;hang&quot;...

    There are several issues with your code.

    First: you didn&#039;t declare a Doctype.

    Second: You use a table for layout. Table DOM is notoriously difficult to handle - aside from the fact that tables aren&#039;t meant for layout.

    Third: You&#039;re inconsistent with your code. A proper HTML element uses double quotes for attributes - you use a combination of single quotes, double quotes, and -worse- NO quotes.

    Fourth: When using the &quot;this.-&quot; identifier, make sure the container has an ID assigned.

    Fifth: Don&#039;t use the anchor element to just call a function. It&#039;s deprecated. Instead, use the onclick/onmouseover event in a span.

    SO:

    &lt;span id=&quot;menu1&quot; onmouseover=&quot;AJAX( &#039;Templates&#039;, ... etc. );&quot;&gt;Category 1&lt;/span&gt;

    instead of the &lt;a href=&quot;#&quot;...&gt;

    Right now, the page actually does an internal refresh because the hyperlink *works*.

    Especially when including JavaScript, proper coding will make things a lot easier to troubleshoot.


  2. In IE7, the page seems to work fine. I can go back and forth over the category links and see changes with no problem. I would suggest adding a &#039;loading&#039; animation before the site launches.

    In Firefox, there is an issue. The items are not even displaying for me.

    Also, please be nice to our fellow answerers. At least someone was going through the trouble to offer suggestions. Maybe the tone was blunt, but we all can set a better example.

Question Stats

Latest activity: earlier.
This question has 2 answers.

BECOME A GUIDE

Share your knowledge and help people by answering questions.