intra-iframe communication example
This commit is contained in:
parent
8e3c2cf920
commit
002521a21b
28
examples/js-get-message-parent.html
Normal file
28
examples/js-get-message-parent.html
Normal file
|
@ -0,0 +1,28 @@
|
|||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head><title>Testing message passing across windows</title>
|
||||
|
||||
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js" type="text/javascript"></script>
|
||||
</head>
|
||||
<body>
|
||||
<p>This is a world program:</p>
|
||||
<iframe id="another-world" src="js-get-message.html"></iframe>
|
||||
|
||||
<br/>
|
||||
<hr/
|
||||
<p>And this a regular JavaScript program:</p>
|
||||
<input id="msg" type="text"/>
|
||||
<input id="submit" type="submit">
|
||||
|
||||
|
||||
<script type="text/javascript">
|
||||
$(document).ready(function() {
|
||||
var otherWorld = document.getElementById("another-world").contentWindow;
|
||||
$("#submit").on("click", function() {
|
||||
otherWorld.postMessage($("#msg").val(), "*");
|
||||
$("#msg").val("");
|
||||
});
|
||||
});
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
30
examples/js-get-message.rkt
Normal file
30
examples/js-get-message.rkt
Normal file
|
@ -0,0 +1,30 @@
|
|||
#lang planet dyoo/whalesong
|
||||
(require (planet dyoo/whalesong/js/world)
|
||||
(planet dyoo/whalesong/js)
|
||||
(planet dyoo/whalesong/web-world))
|
||||
|
||||
;; Test of getting world events from arbitrary JavaScript function application.
|
||||
|
||||
;; We first define a new event handler type, by using make-js-world-event:
|
||||
(define-values (on-message send)
|
||||
(make-js-world-event))
|
||||
|
||||
;; It gives us two values back:
|
||||
;; 1. An event handler that can be passed to big-bang
|
||||
;; 2. A raw JavaScript function that can fire events
|
||||
|
||||
|
||||
;; Let's attach the send-event function to a toplevel function on the window.
|
||||
(void ((js-function (js-eval "function(send) { $(window).bind('message', function(e) { send(e.originalEvent.data); })}"))
|
||||
send))
|
||||
;; js-function lifts JavaScript functions to regular function we can call.
|
||||
|
||||
|
||||
(define (get-message w v msg)
|
||||
(cons msg w))
|
||||
|
||||
|
||||
;; Finally, let's use our big bang:
|
||||
(big-bang '("Initial")
|
||||
(on-message get-message)) ;; Note the on-event here
|
||||
|
Loading…
Reference in New Issue
Block a user