Thank you for being a valued part of the CNET community. As of December 1, 2020, the forums are in read-only format. In early 2021, CNET Forums will no longer be available. We are grateful for the participation and advice you have provided to one another over the years.

Thanks,

CNET Support

General discussion

onClick with links

Aug 1, 2008 2:53AM PDT

Hello all. I'm am pretty new to using javascript, xml, and xmlhttp request. I am trying to use xmlhttp with links. I have tried using onclick with the links and it doesn't work, I have also tried creating a function and putting that in with the link, but it doesn't work. If I create a button, the xmlhttp request works. Is it not possible to use xmlhttp with links, or am I just doing something wrong.

here is the script:
<script type="text/javascript">
var xmlhttp=null;
function loadXMLDoc(url)
{
xmlhttp=null;
if (window.XMLHttpRequest)
{// code for Firefox, Opera, IE7, etc.
xmlhttp=new XMLHttpRequest();
}
else if (window.ActiveXObject)
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
if (xmlhttp!=null)
{
xmlhttp.onreadystatechange=state_Change;
xmlhttp.open("GET",url,true);
xmlhttp.send(null);
}
else
{
alert("Your browser does not support XMLHTTP.");
}
}
function state_Change()
{
if (xmlhttp.readyState==4)
{// 4 = "loaded"
if (xmlhttp.status==200)
{// 200 = "OK"
document.getElementById('data').innerHTML=xmlhttp.responseText;
}
else
{
alert("Problem retrieving data:" + xmlhttp.statusText);
}
}
}
function click(url)
{
onclick="loadXMLDoc(url)"
}
</script>

and here are the links and button(I had to put periods behind the a's so it would show here):
<li><a. href="javascript:click('lwitherspoon.html')">Laurie E. Witherspoon</a></li>
<li><a. href="#" onclick="loadXMLdoc('php/inc/staff/lwitherspoon.html');return false">Quiana Ray</a></li>
<button onclick="loadXMLDoc('php/inc/staff/lwitherspoon.html')">Get XML</button>

Discussion is locked