Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8096585
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T21:29:54+00:00 2026-06-05T21:29:54+00:00

I am sure this is a common question, but since I’m really new to

  • 0

I am sure this is a common question, but since I’m really new to Ajax I couldn’t figure out which solution is the best for me and how to execute this:

I have a page with several links, and a div in the middle of the page in which the output of the ajax request is shown.
By now I have successfully used this ajax function for one link:

function loadXMLDoc()
{
  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("myDiv").innerHTML=xmlhttp.responseText;
    }
  }
  xmlhttp.open("GET","history.html");
  xmlhttp.send();
}

However, I want to show different outputs based on the links clicked.
These are some of my links:

<li class="menu-item"><a href="#" onclick="loadXMLDoc()">Link 1 - History</a></li>
<li class="menu-item"><a href="#">Link 2 - Economics</a></li>
<li class="menu-item"><a href="#">Link 3 - Physics</a></li>

I want to get and show different html files based on the links clicked, But I don’t know how to execute it.

I guess it is done by passing parameters, but I don’t know what to do in order make it work.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T21:29:56+00:00Added an answer on June 5, 2026 at 9:29 pm

    In your case, you could do:

    <li class="menu-item"><a href="#" onclick="loadXMLDoc(1)">Link 1 - History</a></li>
    <li class="menu-item"><a href="#" onclick="loadXMLDoc(2)">Link 2 - Economics</a></li>
    <li class="menu-item"><a href="#" onclick="loadXMLDoc(3)">Link 3 - Physics</a></li>
    

    and:

    function loadXMLDoc(doc) {
        // ...
        xmlhttp.open("GET","history.php?q="+doc);
        // ...
    }
    

    And output a different thing on the server side, depending on the q parameter. (or, do it in JS: if 1, open “page1.html”; if 2, open “page2.html”, etc.


    However, I’d suggest that you don’t reinvent the wheel, and use a JS library such as jQuery; it’s gonna make your life a lot easier. For example, with jQuery, you could just use the following:

    function loadXMLDoc(doc) {
        $.post('ajax/test.html?q=' + doc, function(data) {
            $('#myDiv').html(data);
        });
    }
    

    This might look a bit foreign at first, but it’s much more convenient than what you have. Another highlight is that you can save yourself the onclick attribute and the resulting mixture HTML and JS.

    // no need to use onclick
    $('.menu-item').eq(0).click(function() { loadXML(1); });
    $('.menu-item').eq(1).click(function() { loadXML(2); });
    $('.menu-item').eq(2).click(function() { loadXML(3); });
    

    If you only need to do a few stuff within a project, you save yourself several kilobytes and use single-task libraries from http://microjs.com/# .

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm sure this might be quite common query but couldn't find good answer as
I am sure that this must be a pretty common question but after scouring
I'm sure this must be a common question, but I haven't found an answer
I am sure this must be a common question but I cannot find it
Im sure this question is so easy for all you experts, but I am
I thought this could've been a common question, but it has been very difficult
I'm sure this question has already been asked in one form or another, but
I'm pretty sure that this is the right site for this question, but feel
I know this is a common question, but I just started a project with
This seems like a really amateur question, in my opinion, but nonetheless it's still

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.