国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > HTML > 正文

詳解HTML5中的消息通信代碼

2020-03-24 16:07:14
字體:
來源:轉載
供稿:網友
HTML5支持跨文檔消息通信(Cross-Document Messaging)。

既然使用到消息通信,那么必然有事件(event)產生。根據事件的產生和消費,我們能夠找到發送者和接收者,也就是Sender和Listener。

其中Litener需要做如下的工作:

編寫一個消息處理函數

將消息處理函數注冊:addEventListener( message , function, false);

其中Sender需要做以下工作:

postMessage( this is a message , http://m.survivalescaperooms.com

事件對象event中包含的成員包括:

data:傳遞的數據;

origin:origin,origin包括三要素:主機、協議、端口;

source:來源對象;

好了,下面我們看一個例子,這個例子展示了在頁面中嵌套頁面并且向子頁面發送消息:

父頁面如下:

 !DOCTYPE html  html lang= en  !--  crossDomain.html by Bill Weinman  http://bw.org/contact/  created 2011-04-16 Copyright (c) 2011 The BearHeart Group, LLC This file may be used for personal educational purposes as needed.  Use for other purposes is granted provided that this notice is retained and any changes made are clearly indicated as such.  head  title  HTML5 Messaging Template File (One) /title  link rel= stylesheet type= text/css href= ../CSS/main.css  style  #frameTwo { float: left; width: 500px; height: 400px; margin: 0 5px; padding: 3px; border-top: 2px solid #3c6b92; border-left: 2px solid #3c6b92; border-bottom: 2px solid #ccc; border-right: 2px solid #ccc; #content { height: 500px; } /style  script type= text/javascript  // 域名 var originTwo = http://two.3sn.net  // URL地址 var URLTwo = http://two.3sn.net/H5Msg/ExerciseFiles/Chap01/crossDomainTwo.html  var windowTwo = null; function handleMessage(event) { // 判斷源區域 if (event.origin == originTwo) { if(!windowTwo) windowTwo = event.source; log( message from origin: + event.origin); log(event.data); // 發送消息 windowTwo.postMessage( this is from windowOne! , originTwo); log( message sent back to windowTwo  } else { dispError( message from untrusted origin: + event.origin);
window.addEventListener( message , handleMessage, false); window.onerror = windowErrorHandler; log( this is windowOne log( host: + location.host); // load two頁面 element( frameTwo ).src = URLTwo; // load the frame // ##### Utilities ##### // shortcut for getElementById function element(id) { return document.getElementById(id); } function clearDisp() { element( pageResults ).innerHTML = element( message ).innerHTML = element( message ).className = function dispMessage(message) { m = element( message m.className = message if(m.textContent.length 0) { m.innerHTML += br / + message; } else m.innerHTML = message; function windowErrorHandler(message, filename, lineno) { dispError(message + ( + filename + : + lineno + ) return true; function dispError(errorMessage) { element( pageResults ).innerHTML += errorMessage ? p >
 !DOCTYPE html  html lang= en  !--  crossDomain.html by Bill Weinman  http://bw.org/contact/  created 2011-04-16 Copyright (c) 2011 The BearHeart Group, LLC This file may be used for personal educational purposes as needed.  Use for other purposes is granted provided that this notice is retained and any changes made are clearly indicated as such.  head  title  HTML5 Messaging Template File (Two) /title  link rel= stylesheet type= text/css href= ../CSS/main.css  script type= text/javascript  var originOne = http://one.3sn.net  function handleMessage(event) { if (event.origin == originOne) { log( message from origin: + event.origin); log(event.data); } else { dispError( message from untrusted origin: + event.origin); // ##### Init ##### function init() { window.onerror = windowErrorHandler; // addEventListener doesn t provide the right error object in Firefox window.addEventListener( message , handleMessage, false); log( this is windowTwo  log( host: + location.host); var windowOne = parent; windowOne.postMessage( this is from windowTwo! , originOne); log( message sent to windowOne  // ##### Utilities ##### // shortcut for getElementById function element(id) { return document.getElementById(id); } function clearDisp() { element( pageResults ).innerHTML =  element( message ).innerHTML =  element( message ).className =  function dispMessage(message) { m = element( message  m.className = message  if(m.textContent.length 0) { m.innerHTML += br / + message; } else m.innerHTML = message; function windowErrorHandler(message, filename, lineno) { dispError(message + ( + filename + : + lineno + )  return true; function dispError(errorMessage) { element( pageResults ).innerHTML +=  errorMessage ? p >

需要學習html5的同學請關注php html5視頻教程,眾多html5在線視頻教程可以免費觀看!

以上就是詳解HTML5中的消息通信代碼的詳細內容,其它編程語言

鄭重聲明:本文版權歸原作者所有,轉載文章僅為傳播更多信息之目的,如作者信息標記有誤,請第一時間聯系我們修改或刪除,多謝。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 荔浦县| 阿鲁科尔沁旗| 华安县| 齐齐哈尔市| 精河县| 陵川县| 罗江县| 凌云县| 和政县| 岳西县| 印江| 三河市| 靖边县| 淄博市| 边坝县| 麦盖提县| 乌苏市| 鱼台县| 林西县| 剑川县| 虞城县| 穆棱市| 凤凰县| 新巴尔虎右旗| 西丰县| 邛崃市| 游戏| 黑山县| 永安市| 青岛市| 广灵县| 和顺县| 凯里市| 新安县| 铅山县| 泽普县| 阜平县| 林口县| 尚义县| 尚志市| 乾安县|