xd.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. /**
  2. * @author jianqiang3@staff.sina.com.cn
  3. */
  4. (function(){
  5. var count = 0, timer, callback;
  6. var $E = function(id) {
  7. if (typeof id === 'string') {
  8. return document.getElementById(id);
  9. }
  10. else {
  11. return id;
  12. }
  13. };
  14. function trim(str) {
  15. if(typeof str !== 'string'){
  16. throw 'trim need a string as parameter';
  17. }
  18. if(typeof str.trim === 'function'){
  19. return str.trim();
  20. }else{
  21. return str.replace(/^(\u3000|\s|\t|\u00A0)*|(\u3000|\s|\t|\u00A0)*$/g, '');
  22. }
  23. }
  24. function addEvent (sNode, sEventType, oFunc) {
  25. var oElement = $E(sNode);
  26. if (oElement == null) {
  27. return false;
  28. }
  29. sEventType = sEventType || 'click';
  30. if ((typeof oFunc).toLowerCase() != "function") {
  31. return;
  32. }
  33. if (oElement.attachEvent) {
  34. oElement.attachEvent('on' + sEventType, oFunc);
  35. }
  36. else if (oElement.addEventListener) {
  37. oElement.addEventListener(sEventType, oFunc, false);
  38. }
  39. else {
  40. oElement['on' + sEventType] = oFunc;
  41. }
  42. return true;
  43. }
  44. /**
  45. * query to json
  46. * @param {Object} QS
  47. * @param {Object} isDecode
  48. */
  49. function queryToJson(QS, isDecode) {
  50. var _Qlist = trim(QS).split("&");
  51. var _json = {};
  52. var _fData = function(data){
  53. if(isDecode){
  54. return decodeURIComponent(data);
  55. }else{
  56. return data;
  57. }
  58. };
  59. for(var i = 0, len = _Qlist.length; i < len; i++){
  60. if(_Qlist[i]){
  61. _hsh = _Qlist[i].split("=");
  62. _key = _hsh[0];
  63. _value = _hsh[1];
  64. // 如果只有key没有value, 那么将全部丢入一个$nullName数组中
  65. if(_hsh.length < 2){
  66. _value = _key;
  67. _key = '$nullName';
  68. }
  69. // 如果缓存堆栈中没有这个数据
  70. if(!_json[_key]) {
  71. _json[_key] = _fData(_value);
  72. }
  73. // 如果堆栈中已经存在这个数据,则转换成数组存储
  74. else {
  75. if($.core.arr.isArray(_json[_key]) != true) {
  76. _json[_key] = [_json[_key]];
  77. }
  78. _json[_key].push(_fData(_value));
  79. }
  80. }
  81. }
  82. return _json;
  83. }
  84. function jsonToQuery(JSON,isEncode) {
  85. var _fdata = function(data,isEncode){
  86. data = data == null? '': data;
  87. data = trim(data.toString());
  88. if(isEncode){
  89. return encodeURIComponent(data);
  90. }else{
  91. return data;
  92. }
  93. };
  94. var _Qstring = [];
  95. if(typeof JSON == "object"){
  96. for(var k in JSON){
  97. if(JSON[k] instanceof Array){
  98. for(var i = 0, len = JSON[k].length; i < len; i++){
  99. _Qstring.push(k + "=" + _fdata(JSON[k][i],isEncode));
  100. }
  101. }else{
  102. if(typeof JSON[k] != 'function'){
  103. _Qstring.push(k + "=" +_fdata(JSON[k],isEncode));
  104. }
  105. }
  106. }
  107. }
  108. if(_Qstring.length){
  109. return _Qstring.join("&");
  110. }else{
  111. return "";
  112. }
  113. }
  114. /**
  115. * 初始化监听程序
  116. */
  117. var init = function(){
  118. if (window.postMessage) {
  119. addEvent(window, "message", messageHanlder);
  120. } else {
  121. messagePoll(window, "name");
  122. };
  123. };
  124. /**
  125. * window.name监听方式
  126. * @param {Object} oWindow
  127. * @param {Object} sName
  128. */
  129. var messagePoll = function(oWindow, sName) {
  130. var hash = "";
  131. /**
  132. * 处理处理
  133. * @param {Object} name 返回的window.name
  134. */
  135. function parseData(name) {
  136. var oData = name.split("^").pop().split("&");
  137. return {
  138. domain: oData[0], //域名
  139. data: window.unescape(oData[1]) //数据
  140. }
  141. }
  142. /**
  143. * 获取window.name
  144. */
  145. function getWinName() {
  146. var name = oWindow[sName]; //=window.name
  147. //如果和上次不一样,则获取新数据
  148. if (name != hash) {
  149. hash = name;
  150. messageHanlder(parseData(name));
  151. }
  152. }
  153. timer = setInterval(getWinName, 1000 / 20);
  154. };
  155. /**
  156. * 消息监听函数
  157. * @param {Object} oEvt 监听到的数据对象
  158. */
  159. var messageHanlder = function(oEvt) {
  160. oEvt = oEvt || window.event;
  161. var data = unescape(oEvt.data);
  162. var args = queryToJson(data);
  163. //执行回调
  164. callback && callback(args);
  165. };
  166. init();
  167. /*---------------------------公开函数------------------------------*/
  168. window.XD = {};
  169. /**
  170. * 发送跨域消息
  171. * @param {Object} target //iframe或parent,如果为iframe,则需要传iframe.contentWindow对象
  172. * @param {Object} oArgs
  173. */
  174. XD.sendMessage = function(target, oArgs) {
  175. if(!target) return;
  176. if(typeof target != 'object') return;
  177. var data = jsonToQuery(oArgs);
  178. data = escape(data);
  179. if (window.postMessage) {
  180. // 此处一定要用“*”,否则数据不能返回,参考:http://dev.w3.org/html5/postmsg/#dom-window-postmessage
  181. target.postMessage(data, "*");
  182. } else {
  183. target.name = (new Date()).getTime() + (count++) + "^" + document.domain + "&" + window.escape(data);
  184. }
  185. };
  186. /**
  187. * 接收跨域传递的消息,只需开发者重写即可
  188. * @param {Object} data json数据
  189. */
  190. XD.receiveMessage = function(oCallback) {
  191. callback = oCallback;
  192. };
  193. })();