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
https://forums.phpfreaks.com/topic/146963-novice-ajax-question/
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
https://forums.phpfreaks.com/topic/146963-novice-ajax-question/#findComment-774624
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
https://forums.phpfreaks.com/topic/146963-novice-ajax-question/#findComment-775039
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.