|
| 1 | +jQuery postmessage plugin |
| 2 | +================= |
| 3 | +Modern browsers now support cross-window/cross-domain/cross-origin messaging |
| 4 | +through window.postMessage. |
| 5 | + |
| 6 | +postmessage is a simple wrapper for window.postMessage and the window message |
| 7 | +event, for those who wish to use this feature in a more jQuery-like way. |
| 8 | + |
| 9 | +postmessage falls back to window location hash polling for browsers that do not |
| 10 | +support window.postMessage (i.e., IE7). |
| 11 | + |
| 12 | +postmessage allows messaging back and forth using JSON where window.postMessage |
| 13 | +only allows simple string-based messages. |
| 14 | + |
| 15 | +postmessage also allows for complete request/response roundtrips using |
| 16 | +success/error callbacks modeled after jQuery.ajax. |
| 17 | + |
| 18 | +postmessage is tested on Safari 4, WebKit (Nightlies), Chrome 4, Firefox 3, IE8, IE7 and |
| 19 | +Opera 10.10. |
| 20 | + |
| 21 | + |
| 22 | +API |
| 23 | +=== |
| 24 | + |
| 25 | +$.postmessage(options) |
| 26 | +------------------- |
| 27 | + Send postmessage. |
| 28 | + |
| 29 | + options:Map (Required) |
| 30 | + A set of key/value pairs that configure the postmessage: |
| 31 | + |
| 32 | + target:Window (Required) |
| 33 | + The target window to send the postmessage. |
| 34 | + |
| 35 | + type:String (Required) |
| 36 | + The postmessage type. The postmessage will be dispatched to |
| 37 | + handler(s) bound to postmessages of this type. |
| 38 | + |
| 39 | + data:Object (Required) |
| 40 | + The postmessage data to be sent. |
| 41 | + |
| 42 | + success:Function (Optional) |
| 43 | + A function to be called if the postmessage succeeds. The function gets |
| 44 | + passed the object returned by the postmessage handler. |
| 45 | + |
| 46 | + error:Function (Optional) |
| 47 | + A function to be called if the postmessage fails. The function gets |
| 48 | + passed the object thrown (Exception) by the postmessage handler. |
| 49 | + |
| 50 | + origin:String (Optional) |
| 51 | + The target origin for the postmessage to be dispatched, either as the |
| 52 | + literal string "*" (indicating no preference) or as a URI. The postmessage |
| 53 | + will only be dispatched if the scheme, hostname and port match the |
| 54 | + target origin. Default is "*". |
| 55 | + |
| 56 | + url:String (Optional) |
| 57 | + Required if window.postMessage is not available or using location hash. |
| 58 | + The current url of the target window you are sending the postmessage |
| 59 | + to. This is required for setting the location hash of the target window |
| 60 | + since you may not have access to reading the target window.location. |
| 61 | + |
| 62 | + hash:Boolean (Optional) |
| 63 | + You can force passing postmessages via the target window location |
| 64 | + hash by setting this to true. |
| 65 | + |
| 66 | + |
| 67 | +$.postmessage.bind(type, fn, [origin], [hash]) |
| 68 | +------------------------------------ |
| 69 | + Bind postmessage handler on the current window. |
| 70 | + |
| 71 | + type:String (Required) |
| 72 | + The postmessage type to bind to. |
| 73 | + |
| 74 | + fn:Function (Required) |
| 75 | + The postmessage handler. A function to execute each time the postmessage |
| 76 | + type is received. The function gets passed the postmessage data. |
| 77 | + |
| 78 | + origin:String (Optional) |
| 79 | + A URI specifying the origin of the postmessage. If specified, the postmessage |
| 80 | + handler will only be executed if the postmessage is received from the same |
| 81 | + origin matching the scheme, hostname and port. Otherwise, the postmessage |
| 82 | + sender will be notified through it's error callback of the origin mismatch: |
| 83 | + |
| 84 | + { |
| 85 | + "message": "postmessage origin mismatch", |
| 86 | + "origin": [ |
| 87 | + "http://postmessage.freebaseapps.com", |
| 88 | + "http://www.xyz.com" |
| 89 | + ] |
| 90 | + } |
| 91 | + |
| 92 | + You can set this globally. However, the origin specified in the bind method |
| 93 | + will take precedence. |
| 94 | + |
| 95 | + $.postmessage.origin = "http://www.xyz.com"; |
| 96 | + |
| 97 | + hash:Boolean (Optional) |
| 98 | + You can force location hash polling to check for postmessages by setting |
| 99 | + this to true. This is required only if you are (forcefully) passing postmessages |
| 100 | + via the location hash. |
| 101 | + |
| 102 | + |
| 103 | +$.postmessage.unbind([type], [fn]) |
| 104 | +--------------------------- |
| 105 | + Remove a previously-attached postmessage handler from the current window. If |
| 106 | + type is not specified, all postmessage handlers will be removed. |
| 107 | + |
| 108 | + type:String (Optional) |
| 109 | + The postmessage type to unbind from. If only the type is specified all |
| 110 | + handlers of this type will be removed. |
| 111 | + |
| 112 | + fn:Function (Optional) |
| 113 | + The function that is to be no longer executed. |
0 commit comments