IC SunsetThe developerWorks Connections platform will be sunset on December 31, 2019. On January 1, 2020, this forum will no longer be available. More details available on our FAQ.
  • 2 replies
  • Latest Post - ‏2011-11-14T06:36:00Z by SystemAdmin
2 Posts

Pinned topic changing variable value with onClick

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

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();
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
if (xmlhttp.readyState==4 && xmlhttp.status==200)

<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
    2 Posts

    Re: changing variable value with onClick

    This line above:


    Should read:

  • SystemAdmin
    1140 Posts

    Re: changing variable value with onClick

    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>