When I hit the button in my program the button does not work. How do I get the message to point to this url "http://mweisfeld.1upware.com/joke.txt" when I hit the button? Thanks

<html>

  <head>

    <title>Ajax Joke of the Day Application</title>

    <script language = "javascript" type = "text/javascript">

      var Request = false;

      if (window.XMLHttpRequest) {
        Request = new XMLHttpRequest();
      } else if (window.ActiveXObject) {
        Request = new ActiveXObject("Microsoft.XMLHTTP");
      }

      function retrieveJoke(url, elementID) {

        if(Request) {

          var RequestObj = document.getElementById(elementID);

          Request.open("GET", url);

          Request.onreadystatechange = function()
          {
            if (Request.readyState == 4 && Request.status == 200) {
                RequestObj.innerHTML = Request.responseText;
            }
          }

          Request.send(null);

        }
      }

    </script>

  </head>

  <body>

    <H1>Where do bees go when they get married?</H1>

    <form>
      <input type = "button" value = "Fetch Answer"
        onclick = "retrieveJoke('joke.txt', 'DivTarget')">
    </form>

    <div id="DivTarget"> </div>

  </body>


</html>

Recommended Answers

All 6 Replies

if you want the browser to actually navigate to joke.text then change your form to:

<form method="post" action="joke.txt">
      <input type = "submit" value = "Fetch Answer">
    </form>

Thanks Hielo, it worked but if I may. How do I get the response from when I hit the button to appear on the same page as the button, instead of opening in another window?

Thanks Heilo! Yes, and no, the response when you click the fetch button "On a honeymoon" opens in another window, I want the answer to appear under the fetch button like this:

Where do bees go on when they get married?

(Fetch Button)

On a honeymoon(Answer)

(I want this answer "On a honeymoon" to appear under the fetch button after you click it, not in another window if possible. Any suggestions?

Thanks for your help, I figured it out.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.