AJAX demo——操作文字檔

來源:互聯網
上載者:User

<script language="javascript">
//建立AJAX
function initxmlhttp()
{
   var xmlhttp
   try {
       xmlhttp=new ActiveXObject("Msxml2.XMLHTTP");
   } catch (e) {
       try {
           xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
       } catch (E) {
           xmlhttp=false;
       }
   }
   if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
       try {
           xmlhttp = new XMLHttpRequest();
       } catch (e) {
           xmlhttp=false;
       }
   }
   if (!xmlhttp && window.createRequest) {
       try {
           xmlhttp = window.createRequest();
       } catch (e) {
           xmlhttp=false;
       }
   }
   return xmlhttp;
}
//從文字檔中讀取檔案並且顯示
function readText()
{
   var readButton=document.getElementById('read');
   var showcontent=document.getElementById('content');
   readButton.disabled="disabled";
   showcontent.innerHTml='正在讀取,Loading.....';
   var xmlhttp=initxmlhttp();
   var url="operation.php?action=read";
   xmlhttp.open("GET",url,true);
   xmlhttp.onreadystatechange=function(){
     if(xmlhttp.readyState==4 && xmlhttp.status==200)
     {
        showcontent.innerHTML=xmlhttp.responseText;
        readButton.disabled="";
        document.getElementById('edit').disabled="";
     }
   }
   xmlhttp.send(null);
}
//轉換到編輯模式
function edit()
{
document.getElementById('edit').disabled="disabled";
var str='';
str+='<textarea name="textContent" cols="50" rows="8" id="textContent">';
str+='</textarea>';
document.getElementById('content').innerHTML=str;
document.getElementById('update').disabled="";
}
//更新資料到文字檔
function update()
{
var xmlhttp=initxmlhttp();
var url="operation.php?action=write";
var data="content="+document.all.textContent.value;
xmlhttp.open("POST",url,true);
xmlhttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
xmlhttp.send(data);
xmlhttp.onreadyStatechange=function(){
     if(xmlhttp.readyState==4 && xmlhttp.status==200)
     {
         document.getElementById('content').innerHTML=xmlhttp.responseText;
         document.getElementById('update').disabled="disabled";
     }
}
}
</script>
<body>
<div class="main-box">
<div class="head-dark-box">AJAX demo——操作文字檔</div>
<div class="body-box">
   <div class="tip-msg">一個簡單的AJAX示範。主要功能有 :<br />
     1、讀取文字檔並顯示。<br />
     2、更新文字檔。
   </div>   
   <div class="alt-table" ><br />
     <input id="read" name="read" type="button" class="button" value="讀取" onclick="readText()" />
     <input id="edit" disabled="disabled" name="edit" type="button" class="button" value="編輯" onclick="edit()" />
     <input id="update" disabled="disabled" name="update" type="button" class="button" value="更新" onclick="update()" />
     <div id="content" class="textbox-title"></div>
   </div>
</div>
<div id="copyright" class="foot-sql">Copyright @ 2006 <a href="http://www.phpobject.net">www.phpobject.net</a>!</div>
</div>
</body>

以下是asp code:
<%
set fso=server.CreateObject("Scripting.FileSystemObject")

if Request.QueryString("action")="read" then
    Set txtFile=fso.OpenTextFile(Server.MapPath("demo.txt"))
    Response.Write "<h3>text.txt content is following:</h3>"
    Response.Write "<hr width='100%' color='#cc9999'>"
    Response.Write "<PRE>"
    While Not txtFile.AtEndOfStream
        Response.Write txtFile.ReadLine & "<br>"
    Wend
    Response.Write "<PRE>"
else
    Set txtFile=fso.OpenTextFile(Server.MapPath("demo.txt"),2,true)
    txtFile.WriteLine Request("content")
end if
txtFile.Close
%>

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.