Cross Document messaging 통신
다른 사이트 페이지끼리 통신 하기


참조 URL
  1. https://developer.mozilla.org/en-US/docs/Web/API/window.postMessage
  2. http://m.mkexdev.net/75
  3. http://jjeong.tistory.com/476
  4. postMessage를 이용한 크로스도메인간 iframe 리사이징
  5. http://jsonobject.tistory.com/53





[그림1] postMessage를 통한 크로스 사이트 통신



 '그림1'에서와 같이 페이지에서 iFrame로 임베이딩된 상태에서는 브라우저의 보안 정책으로 인해 자바스크립트를 통해 페이지간 통신을 할 수 없었다. 하지만 postMessage를 통해서 가능하게 되었다. 지원하는 브라우저는 아래 '그림2'에서와 같이 IE8에서부터 지원이 되므로 거의 대부분의 브라우저에서 지원된다고 보면 될 것이다.



[그림2] postMessage 브라우저 호환표

http://caniuse.com/#feat=x-doc-messaging )


 이제 postMessage의 사용법에 대해서 알아 보자



데이터 송신하기


데이터를 송신하기 위해서는 window 객체의 postMessage 함수를 사용하면 된다


[코드1] 데이터 송신



data: 전달할 메세지, 즉 송신할 데이터를 지정한다

ports: 메세지를 전송할 포트(생략 가능)

targetOrigin: 타켓 도메인,  즉 메세지를 수신받는 도메인을 지정한다 대상이 특정 도메인이 아니라면 * 로 지정한다 




데이터 수신하기


데이터를 수신하는 측에서는 단순히 window 객체의 message 이벤트를 구현하면 된다


[코드2] 데이터 수신



data: 메세지 즉 송신한 데이터이다

origin: 메세지를 보내는 곳의 도메인 정보 크로스 도메인 환경이라면 상대 도메인을 확인하는 것이 보안상 좋다

source: 메세지를 보내는 윈도우 객체 



이제 이 코드를 가지고 아래 예제 코드로 활용법을 보도록 하자.



[코드] 송신쪽 페이지 코드



[코드] 수신쪽 페이지 코드



Syntax

otherWindow.postMessage(message, targetOrigin, [transfer]);



otherWindow

A reference to another window; such a reference may be obtained, for example, using the contentWindow property of an iframe element, the object returned by window.open, or by named or numeric index on window.frames.


message

Data to be sent to the other window.


targetOrigin

Specifies what the origin of otherWindow must be for the event to be dispatched, either as the literal string "*" (indicating no preference) or as a URI. If at the time the event is scheduled to be dispatched the scheme, hostname, or port of otherWindow's document does not match that provided in targetOrigin, the event will not be dispatched; only if all three match will the event be dispatched. This mechanism provides control over where messages are sent; for example, if postMessage were used to transmit a password, it would be absolutely critical that this argument be a URI whose origin is the same as the intended receiver of the message containing the password, to prevent interception of the password by a malicious third party. Always provide a specific targetOrigin, not *, if you know where the other window's document should be located. Failing to provide a specific target discloses the data you send to any interested malicious site.


transfer [Optional]

Is a sequence of Transferable objects that are transferred with the message. The ownership of these objects is given to the destination side and they are no more usable on the sending side.












CSS Transition를 이용한 버튼 애니메이션 효과


참조 URL
  1. http://html5snippets.com/snippets/59-introduction-to-css3-animations
  2. http://www.the-art-of-web.com/css/css-animation/#.Ufm1yJLxqlg
  3. http://www.creativebloq.com/css3/animation-with-css3-712437
  4. http://lab.tylergaw.com/themanfromhollywood/
  5. http://cssdeck.com/labs/google-doodle-in-css-follow-up
  6. http://www.clicktorelease.com/code/css3dclouds/
  7. http://tympanus.net/Tutorials/AnimatedButtons/index2.html
  8. http://tympanus.net/Tutorials/OriginalHoverEffects/index.html
  9. http://webdesign.tutsplus.com/tutorials/htmlcss-tutorials/css3-transitions-and-transforms-from-scratch/
  10. http://2012.beercamp.com/
  11. http://www.creativesandbox.com/guidebook


 HTML5가 나오면서 CSS로 애니메이션을 할 수 있는 방법이 나오게 되었다. 이전에는 Javascript로 구현해야 하였기에 많은 CPU와 복잡한 로직으로 되어야 했지만 CSS의 Taansition을 통해 그나마 쉽게 컨트롤 할 수 있게 되었다. 이와 관련해서 여러가지 예제와 샘플들이 있지만 아래와 같이 버튼에 대해서 효과를 주는 간단한 CSS를 소개해 주고자 한다.



CSS의 Transition을 지원하는 브라우저에서만 확인할 수 있다. 예) 최신 Chrome browser
박스를 마우스 오버 시키면 박스가 빠르게 회전을 하고
마우스 Leave 시키면 반대편으로 빠르게 회전하여 돌아오는 CSS Transition 데모다.
  • A
  • B








위 표현은 아래 코드를 통해서 나타낸다.




[코드] HTML의 Style 선언




[코드] HTML의 Tag 선언



 HTML 5의 CSS3 Transition에 가장 잘 표현한 사이트가 있으니 여기에서 확인해 보고 경이로움을 같이 공유하였으면 합니다.

http://www.clicktorelease.com/code/css3dclouds/



그리고 어린이 도서와 같은 팝업북 같은 느낌의 사이트

http://www.creativesandbox.com/guidebook - 강추






ES5 & ES6 ( ECMAScript 5 & ECMAScript 6)


참조 URL
  1. http://www.ecma-international.org/publications/files/ECMA-ST/Ecma-262.pdf
  2. http://kangax.github.io/es5-compat-table/
  3. http://kangax.github.io/es5-compat-table/es6/




 ECMAScript 5를 지원하는 브라우저와 ECMAScript 6를 지원하는 브라우저 호환성을 나타내는 표다.






[그림1] ES5 호환 테이블 http://kangax.github.io/es5-compat-table/





[그림2] ES6 호환 테이블 http://kangax.github.io/es5-compat-table/es6/




Cross-Origin Resource Sharing

참고 URL
  1. http://www.w3.org/TR/cors/
  2. http://blog.iolo.kr/494



 Ajax를 사용하기 위해서는 "Same Origin Policy"라는 원칙 때문에 현재 페이지의 HTML을 내려준 웹 서버에게만 Ajax 요청을 보낼 수 있다. 그렇지만 OpenAPI(매시업)나 대규모 사이트가 개발되는 환경에서는 Ajax를 통해 할 수 있는 일에 제약을 받게 된다. 그래서 한참 뒤에야 W3C는 크로스도메인간에도 Ajax 요청을 할 수 있는 방법을 표준화 하게 되었다. 그것이 바로 "CORS(Cross-Origin Resource Sharing)"다. 아래 표를 보고 이해를 돕도록 하자




1. www.xxx.com/request.html 페이지에서 Ajax로 www.yyy.com/server.aspx URL로 요청


2. www.yyy.com/server.aspx - ajax 요청에 응답 함(json 리턴 함)
   ( 헤더에 특정 값으 세팅 함 - header : 'Access-Control-Allow-Origin:www.xxx.com 또는 *' )


[표1] CORS 기본 환경


 위와 같은 환경은 크로스도메인이나 "표1"의 2번 사항과 같이 세팅되어 있으면 Ajax로 호출하여 원하는 값을 받을 수 있다. 만약 '*'로 세팅되어 있으면 어느 사이트에서 ajax 요청을 해도 응답을 받을 수 있게 되었다.


자세한 사항은 'http://www.w3.org/TR/cors/' 에서 확인해 보자.






+ Recent posts