「AJAX」XMLHttpRequestオブジェクトのサンプル

2020年11月26日

構文
variable=new XMLHttpRequest();
JSコード

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<script>
function loadXmlFunc()
{
var xmlhttp;
if (window.XMLHttpRequest)
{
// IE7+, Firefox, Chrome, Opera, Safari browser run code
xmlhttp=new XMLHttpRequest();
}
else
{
// IE6, IE5 browser run code
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("myDiv").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","/try/ajax/ajax_info.txt",true);
xmlhttp.send();
}
</script>
<script> function loadXmlFunc() { var xmlhttp; if (window.XMLHttpRequest) { // IE7+, Firefox, Chrome, Opera, Safari browser run code xmlhttp=new XMLHttpRequest(); } else { // IE6, IE5 browser run code xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); } xmlhttp.onreadystatechange=function() { if (xmlhttp.readyState==4 && xmlhttp.status==200) { document.getElementById("myDiv").innerHTML=xmlhttp.responseText; } } xmlhttp.open("GET","/try/ajax/ajax_info.txt",true); xmlhttp.send(); } </script>
<script>
function loadXmlFunc()
{
      var xmlhttp;
      if (window.XMLHttpRequest)
      {
            //  IE7+, Firefox, Chrome, Opera, Safari browser run code
            xmlhttp=new XMLHttpRequest();
      }
      else
      {
            // IE6, IE5 browser run code
            xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
      }
      xmlhttp.onreadystatechange=function()
      {
            if (xmlhttp.readyState==4 && xmlhttp.status==200)
            {
                  document.getElementById("myDiv").innerHTML=xmlhttp.responseText;
            }
      }
      xmlhttp.open("GET","/try/ajax/ajax_info.txt",true);
      xmlhttp.send();
}
</script>

htmlコード

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<div id="myDiv"><h2>use AJAX update contents</h2></div>
<button type="button" onclick="loadXmlFunc()">update contents</button>
<div id="myDiv"><h2>use AJAX update contents</h2></div> <button type="button" onclick="loadXmlFunc()">update contents</button>
<div id="myDiv"><h2>use AJAX update contents</h2></div>
<button type="button" onclick="loadXmlFunc()">update contents</button>

 

Ajax

Posted by arkgame