Jump to content

novice ajax question


alwaysme

Recommended Posts

Hello there,

 

I find a little script on the net for ajax which loads hello.html (pageone), but lets say i want another link that loads statistics.html, how do i do that? this is the code

 

<script type="text/javascript">
var http = false;

if(navigator.appName == "Microsoft Internet Explorer") {
  http = new ActiveXObject("Microsoft.XMLHTTP");
} else {
  http = new XMLHttpRequest();
} 

function replace() {
  http.open("GET", "hello.html", true);
  http.onreadystatechange=function() {
    if(http.readyState == 4) {
      document.getElementById('pageone').innerHTML = http.responseText;
    }
  }
  http.send(null);
}
</script></head>
<body>
<p><a href="javascript:replace()">page one</a></p>

<div id="pageone">
  Hello, world!
</div>
</body>

 

like i need another link (maybe more later) where each load different page, now i can only load one page with the above...

 

i.e

<p><a href="javascript:replace()">page one</a></p>  // this takes me to hello.html (working)
<p><a href="javascript:replace()">page two</a></p> //this take me to statistics.html

 

 

 

how can i make that?

Link to comment
Share on other sites

Hey Always

 

<script type="text/javascript">
var http = false;

if(navigator.appName == "Microsoft Internet Explorer") {
  http = new ActiveXObject("Microsoft.XMLHTTP");
} else {
  http = new XMLHttpRequest();
}

function replace() {
  http.open("GET", "hello.html", true);
  http.onreadystatechange=function() {
    if(http.readyState == 4) {
      document.getElementById('pageone').innerHTML = http.responseText;
    }
  }
  http.send(null);
}

function replace2() {
  http.open("GET", "statistics.html", true);
  http.onreadystatechange=function() {
    if(http.readyState == 4) {
      document.getElementById('pageone').innerHTML = http.responseText;
    }
  }
  http.send(null);
}
</script></head>
<body>
<p><a href="javascript:replace()">page one</a></p>
<p><a href="javascript:replace2()">page one</a></p>

<div id="pageone">
  Hello, world!
</div>
</body>

Link to comment
Share on other sites

Blerh....

 

 

Forgot about this thread.

 

 

Variables in Javascript are very, very, very straight forward.

 

 

function somefunction(somevar) {

    //somevar is now what ever value is passed into the function

}

 

 

function replace(url) {

    http.open("GET", url, true);

    //blah

}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.