模仿線上電影AJAX評論

來源:互聯網
上載者:User

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 <html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>視頻播放-ajax評論</title>
<style>
 #comment{
  width:500px;
  height:400px;
  background-color:#FC6;
  }
</style>


<!-- 此部分已封裝-->
<script src="ajaxUtil.js" language="javascript" type="text/javascript"></script>

<script language="javascript" type="text/javascript">


function test(){
  /*
  ajax無重新整理的評論技術
  1、發布的評論,無重新整理的顯示到下面的地區
  2、通過ajax把內容存到伺服器上
  3、擷取伺服器的資料,顯示到評論地區
  */
  var comment=$$("com");
  //初始化url
  var url="ajax-youku-server.php";
  var params="comment="+encodeURI(comment.value);

   get(url,params,processCommentData);
}

 

function processCommentData(xhr){
   
    //擷取div的對象
    var div=document.getElementById("comment");
   
    //產生一個段落標記
    var p=document.createElement("p");
   
    //產生一個文本節點
    var text=document.createTextNode("評論內容:"+xhr.responseText);
    //將文本添加到p元素  <p>text</p>
    p.appendChild(text);
   
    //將段落標記追加到div中
    div.appendChild(p);
}

 

</script>

 


</head>

<body>
優酷
<hr>
<!-- 此部分模仿視頻,只不過是換成了SWF-->
<script type="text/javascript">
swfobject.registerObject("FlashID");
</script>
<object id="FlashID" classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" width="960" height="90">
  <param name="movie" value="b3d2d01e7b0c2fcdc92dc04af9f3f5b1[1].swf" />
  <param name="quality" value="high" />
  <param name="wmode" value="opaque" />
  <param name="swfversion" value="8.0.35.0" />
  <!-- 此 param 標籤提示使用 Flash Player 6.0 r65 和更高版本的使用者下載最新版本的 Flash Player。如果您不想讓使用者看到該提示,請將其刪除。 -->
  <param name="expressinstall" value="Scripts/expressInstall.swf" />
  <!-- 下一個對象標籤用於非 IE 瀏覽器。所以使用 IECC 將其從 IE 隱藏。 -->
  <!--[if !IE]>-->
  <object type="application/x-shockwave-flash" data="b3d2d01e7b0c2fcdc92dc04af9f3f5b1[1].swf" width="960" height="90">
    <!--<![endif]-->
    <param name="quality" value="high" />
    <param name="wmode" value="opaque" />
    <param name="swfversion" value="8.0.35.0" />
    <param name="expressinstall" value="Scripts/expressInstall.swf" />
    <!-- 瀏覽器將以下替代內容顯示給使用 Flash Player 6.0 和更低版本的使用者。 -->
    <div>
      <h4>此頁面上的內容需要較新版本的 Adobe Flash Player。</h4>
      <p><a href="http://www.adobe.com/go/getflashplayer"><img src=http://www.bkjia.com/uploads/allimg/131201/06395S504-0.gif" alt="擷取Adobe Flash Player" width="112" height="33" /></a></p>
     </div>
    <!--[if !IE]>-->
  </object>
  <!--<![endif]-->
</object>
<hr>

<textarea cols="45" id="com" rows="4"></textarea><br>
<input type="button" value="俺說兩句" onclick="test()"/>

<hr />
<div id="comment"></div>
<script type="text/javascript">
swfobject.registerObject("FlashID");
swfobject.registerObject("FlashID");
</script>
</body>
</html>

伺服器端代碼:
<?php
//省略了插入資料庫的步驟

echo $_GET['comment'];

 ?>


封裝部分代碼:
function get(url1,params,methodName){
  /*
 ajax使用的基本步驟:

 1、初始化ajax引擎
 2、封裝url(設定要請求的路徑)
 3、開啟ajax引擎(同步方式、非同步方式;本次傳輸使用get還是post)
 4、將要請求的資訊通過引擎發送到伺服器進行處理
 5、監聽伺服器返回給ajax引擎的處理狀態
 6、判斷是否互動完畢,如果互動完畢則取出返回的數
  */   
  //初始化ajax引擎
  var xhr = new XMLHttpRequest();//這種方式只針對ie瀏覽器,並且ie6以下還有問題。
  var url=url1+"?"+params+"&r="+Math.random();

  //alert(url);
  //開啟引擎
  xhr.open("get",url,true);   //readyState=1
 
  //發送請求
  xhr.send(null);   //readyState=2
 
  //監聽readyState值的改變,每次改變都會執行下面額函數 
  xhr.onreadystatechange=function (){
     
      //如果等於4,表明互動完畢 ,我們可以取出伺服器返回的內容
      if(xhr.readyState==4){
           
    //動態調用方法,為什麼說是動態呢?方法的名稱是個變數methodName
            methodName(xhr);
           
      }
     
  }

}

//$$()方法用於方便取出 id="id" 的對象
function $$(id){
   
    return document.getElementById(id);
}


摘自 劉海闖的專欄

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.