Last active
December 10, 2015 05:48
-
-
Save latpaw/4389865 to your computer and use it in GitHub Desktop.
a new simple xhr post data
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE HTML> | |
<html lang="en-US"> | |
<head> | |
<meta charset="UTF-8"> | |
<title></title> | |
</head> | |
<body> | |
<div id="form"> | |
<form method="post" name="form"> | |
<input type="text" name="name" id="input" /> | |
<input type="submit" value="submit" id="sub"/> | |
</form> | |
<!-- <p id="a">a</p> --> | |
</div> | |
<script type="text/javascript"> | |
var sub = document.getElementById("sub"); | |
var form = document.getElementById("form"); | |
// var a = document.getElementById("a"); | |
function cxhr(){ | |
if(window.ActiveXObject){ | |
return new ActiveXObject("Microsoft.XMLHTTP"); | |
}else if(window.XMLHttpRequest){ | |
return new XMLHttpRequest(); | |
} | |
} | |
sub.onclick=function(ev){ | |
var xhr = cxhr(); | |
if(ev.preventDefault){ | |
ev.preventDefault();//or firefox not work | |
}else{ev.returnValue=false;} | |
var url="m.php"; | |
var params="name="+document.getElementById("input").value; | |
xhr.open("POST",url,true); | |
xhr.setRequestHeader("Content-type","application/x-www-form-urlencoded;charset:UTF-8"); | |
// xhr.setRequestHeader("Content-length",params.length); | |
// xhr.setRequestHeader("Connection","close"); | |
xhr.onreadystatechange = function(){ | |
// console.log(xhr.readyState); | |
if(xhr.readyState == 4 && xhr.status == 200){ | |
var b = xhr.responseText; | |
form.innerHTML = b; | |
form.style.opacity="0"; | |
var op=0; | |
var o = setInterval(function(){op=op+0.1;if(op>=1){op=1;window.clearInterval(o);}form.style.opacity = op;},100) | |
} | |
}; | |
xhr.send(params); | |
} | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment