Topic
  • 2 replies
  • Latest Post - ‏2011-11-14T06:36:00Z by SystemAdmin
pagerussell
pagerussell
2 Posts

Pinned topic changing variable value with onClick

‏2011-11-14T01:24:50Z |
hello,

I am building a site where I want all links to partial page reload using xmlhttprequest and innerhtml. I have done this before, butI always had to have a seperate xmlhttprequest function for each link. So this time I am trying to write just one function, and have the onClick change which document the request is getting. It is not working and I appear to novice to figure out why. Here is the code, i have bolded some relevant lines:

<script type="text/javascript">

function ChangeContent()
{
var whichPHP;
var xmlhttp;
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById('maincontentbox').innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET",*whichPHP+".php",*true);
xmlhttp.send();
}
</script>

<a href="javascript: ChangeContent()" onclick="whichPHP='contact_us'">

am I making a syntax error somewhere, or just screwing the whole thing up altogether?

The website is www.andymacislove.org if that helps at all.

thanks, page

Also, if I use the innerhtml function to write another of these hrefs, will that link be able to access the function if it is in the header, or should I put the entire function in a .js file on the server? The point of all this would be that the entire page never reloads and all the text content is swapped out on demand.
Updated on 2011-11-14T06:36:00Z at 2011-11-14T06:36:00Z by SystemAdmin
  • pagerussell
    pagerussell
    2 Posts

    Re: changing variable value with onClick

    ‏2011-11-14T01:27:37Z  
    This line above:

    xmlhttp.open("GET",*whichPHP+".php",*true);

    Should read:

    xmlhttp.open("GET",whichPHP+".php",true);
  • SystemAdmin
    SystemAdmin
    1140 Posts

    Re: changing variable value with onClick

    ‏2011-11-14T06:36:00Z  
    Change the handler to accept an input parameter wherePHP, instead.
    
    function ChangeContent(whichPHP) 
    { 
    //var whichPHP; var xmlhttp; 
    //etc etc... (the same) 
    }
    

    The anchor element can written like this.
    
    <a href=
    "javascript:void(0)" onclick=
    "ChangeContent('contact_us')"> Contact Us </a>