xmlDoc,xmlHttp

11869 단어 htmlxmlAjaxIEMicrosoft

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
	<head>
		<script type="text/javascript">
 <!--
 var xmlHttp ;//        
 function createXMLHttpRequest(){
 if(window.ActiveXObject){
 xmlHttp = new ActiveXObject("Microsoft.XMLHTTP") ;
 }
 else if(window.XMLHttpRequest){
 xmlHttp = new XMLHttpRequest() ;
 }
 }
 function checkUser(){
 createXMLHttpRequest() ;
 var name = document.getElementById("username").value ;
 var password = document.getElementById("password").value ;
 var url = "servlet/Login_check?username="+name+"&password="+password ;
 xmlHttp.open("GET",url,true) ;
 xmlHttp.onreadystatechange = showResult ;
 xmlHttp.send(null) ;
 }
 //    
 function showResult(){
 if(xmlHttp.readyState == 4){
 if(xmlHttp.status == 200){
 var result = xmlHttp.responseText;//           
 alert("result:"+result) ;
 if(result=="OK"){
    alert("if    ") ;
       window.location.href ="test.html?name=test" ;
 }else{
 document.getElementById("checkResult").innerHTML="<b>"+result+"</b>" ;
 }
 }
 }
 }
 //-->
 </script>
	</head>
	<body>
		ajax
		<br>
		username:
		<input type="text" id="username">
		<div id="checkResult"></div>
		<p>
			password:
			<input type="password" id="password">
		<p>
			<input type="button" value="check" onclick="checkUser()">
		<p>
	</body>
</html>

----------------------------------------------------

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
	<head>
		<title>test.html</title>

		<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
		<meta http-equiv="description" content="this is my page">
		<meta http-equiv="content-type" content="text/html; charset=UTF-8">

		<!--<link rel="stylesheet" type="text/css" href="./styles.css">-->

	</head>
	<script type="text/javascript">
	//      XML
	var xml = null;
	var CurrentDate;
	/*   XML   */
	function loadXmlDoc(xmlUrl) {
		try {
			//  IE xml  
			xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
		} catch (e) {
			try {
				//  FF xml  
				xmlDoc = document.implementation.createDocument("", "", null);
			} catch (e) {
				alert(e.message)
			}
		}
		try {
			//      
			xmlDoc.async = false;
			xmlDoc.load(xmlUrl);
			return (xmlDoc);
		} catch (e) {
			alert(e.message)
		}
		return (null);
	}

	function getPropValues() {

		var nodeName = document.getElementById("nodeName").value;
		var propName = document.getElementById("propName").value;
		alert(propName + " 。");
		if (nodeName == "") {
			alert("     ");
			return;
		}

		if (xml == null) {
			xml = loadXmlDoc("data.xml");
			alert("DOM     ");
		}
		//                Element  
		var nodes = xml.getElementsByTagName(nodeName);

		if (nodes == null) {
			document.getElementById("result").innerText = "          ";
			return;
		}

		var results = "";
		if (propName != "" && nodes[0].getAttribute(propName) == null) {
			document.getElementById("result").innerText = "             ";
			return;
		}
		for ( var i = 0; i < nodes.length; i++) {
			if (propName != null && propName != "") {
				results += nodes[i].getAttribute(propName) + "
"; } else { alert(" ~"); results += nodes[0].childNodes[0].nodeValue + "
"; //childNodes Node 。 , 。 } } document.getElementById("result").innerHTML = results; } // xpath xml function readXmlNode(node, xpath) { try { if (node == null || xpath == null) { return null; } else { var result = node.selectSingleNode(xpath); if (result == null) { return null; } else { return result; } } } catch (e) { return null; } } // xpath xml function readXmlAttrValue(node, xpath, attrName) { try { if (node == null || xpath == null) { return null; } else { var result = node.selectSingleNode(xpath); if (result == null) { return null; } else { return result.getAttribute(attrName); } } } catch (e) { return null; } } /*-----------------------------------------------------------------------*/ function readTime1() { var date = new Date(); CurrentDate = ChangeTimeToString(date); //alert("hello "); //alert(CurrentDate); var t = setTimeout("alert(CurrentDate)", 5000); } function readTime2() { var date = new Date(); CurrentDate = ChangeTimeToString(date); //alert(CurrentDate); var t = setInterval("alert(CurrentDate)", 5000); } // yyyy-MM-dd hh:mm function ChangeTimeToString(DateIn) { var Year = 0; var Month = 0; var Day = 0; var Hour = 0; var Minute = 0; var CurrentDate = ""; // Year = DateIn.getYear(); Month = DateIn.getMonth() + 1; Day = DateIn.getDate(); Hour = DateIn.getHours(); Minute = DateIn.getMinutes(); CurrentDate = Year + "-"; if (Month >= 10) { CurrentDate = CurrentDate + Month + "-"; } else { CurrentDate = CurrentDate + "0" + Month + "-"; } if (Day >= 10) { CurrentDate = CurrentDate + Day; } else { CurrentDate = CurrentDate + "0" + Day; } if (Hour >= 10) { CurrentDate = CurrentDate + " " + Hour; } else { CurrentDate = CurrentDate + " 0" + Hour; } if (Minute >= 10) { CurrentDate = CurrentDate + ":" + Minute; } else { CurrentDate = CurrentDate + ":0" + Minute; } return CurrentDate; } /*------------------------------------------------------------------*/ function writeStream() { var fso, tf; fso = new ActiveXObject("Scripting.FileSystemObject"); tf = fso .CreateTextFile("D:/workspace/studyTest/WebRoot/test.txt", true); var textArea = document.getElementById("result") tf.WriteLine(textArea.innerText); tf.WriteBlankLines(2); tf.Close(); } /*---------------------------------------------------------------------*/ function findAll() { if (xml == null) { xml = loadXmlDoc("data.xml"); //alert("DOM "); } var results = ""; var nodes = xml.documentElement;// var childs = nodes.childNodes;// for ( var x = 0; x < childs.length; x++) { if (childs[x].nodeType == 1) { var child = childs[x].childNodes; // for ( var y = 0; y < child.length; y++) { var arrylist = child[y].attributes;// if (arrylist.length <= 0) { results += child[y].nodeName + ":" + child[y].childNodes[0].nodeValue + "
"; } else { for ( var z = 0; z < arrylist.length; z++) { //document.write(child[y].nodeName + "
"); //alert(arrylist[z].nodeName); //alert(child[y]) results += child[y].nodeName+":"+arrylist[z].nodeName + ":" + arrylist[z].nodeValue + "
"; //arrylist[z].text } } } } } document.getElementById("result").innerHTML = results; } /*---------------------------------------------------------------------------------------------------------------------*/ function getValueByNode() { if (xml == null) { xml = loadXmlDoc("data.xml"); alert("DOM "); } var nodeName = document.getElementById("nodeName").value; alert("nodeName:" + nodeName); var propName = document.getElementById("propName").value; alert("propName:" + propName); var results = ""; // nodeName if (nodeName != null && "" != nodeName) { var nodes = xml.getElementsByTagName(nodeName); alert("nodes :" + nodes.length); if (propName != null && "" != propName) { for ( var x = 0; x < nodes.length; x++) { results += propName + ":" + nodes[x].getAttribute(propName) + "
"; } } else { results += nodeName + ":" + nodes[0].childNodes[0].nodeValue + "
"; } } else { alert(" "); } document.getElementById("result").innerText = results; } // function getParameter() { // url var url = window.location.href; alert("url:"+url); var aa = url.indexOf('='); alert("aa:"+aa); if (aa == -1) { return ""; } else { url = url.substring(aa + 1); window.alert(url); } return url; } function showView() { var url = getParameter(); document.getElementById("showView").innerText = " ," + url; } </script> <body onload=showView()> <div id="showView"></div> : <input type="text" id="nodeName" /> <br /> : <input type="text" id="propName" /> <br /> <input type="button" value=" " onclick="getValueByNode()" /> <br /> <textarea id="result" style="width: 200px; height: 60px;"></textarea> <br /> <button onclick="readTime1()"> 1 </button> <button onclick="readTime2()"> 2 </button> <button onclick="writeStream()"> </button> <button onclick="findAll()"> XML </button> </body> </html>

좋은 웹페이지 즐겨찾기