欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Ajax -- 获取XMLHttpRequest对象

程序员文章站 2022-07-12 18:58:20
...
<body>
     <center>
           <button id="btn01">获取XMLHttpRequest对象</button>
           <form action="AjaxRequestServlet" method="post">
               <input type="text" name="username" /><input type="submit" value="submit" />
           </form>
     </center>
</body>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<style type="text/css">button {width: 200px;}</style>
<script type="text/javascript">
     //获取 XMLHttpRequest
     function getRequest(){
           //创建XMLHttpRequest 对象
           var xmlHttpRequest = null;
           try{//试用与主流浏览器
                xmlHttpRequest = new XMLHttpRequest();
           }catch(e){//适用于IE5、IE6
                try{
                     xmlHttpRequest = new ActiveXObejct("Microsoft.XMLHTTP");
                }catch(e){
                     alert("your browser not support Ajax!");
                }
           }
           return xmlHttpRequest;
     };
     var request = getRequest();
     //页面加载完成后加载
     window.onload = function(){
           //document.getElementById("btn01").onclick: 获取到 btn01 点并添加点击事件
           document.getElementById("btn01").onclick = function(){
                alert(request);
           };
     };
</script>
</head>
相关标签: ajax