From f25480dec51e41411078fc962f0255b534bd3bca Mon Sep 17 00:00:00 2001 From: Cheng Zhao Date: Wed, 7 May 2014 09:45:39 +0800 Subject: [PATCH] :memo: Say more on sending messages from browser to web page. --- docs/api/browser-window.md | 33 +++++++++++++++++++++++++++++++++ docs/api/ipc-browser.md | 9 ++++++--- 2 files changed, 39 insertions(+), 3 deletions(-) diff --git a/docs/api/browser-window.md b/docs/api/browser-window.md index e31348e..e2966dc 100644 --- a/docs/api/browser-window.md +++ b/docs/api/browser-window.md @@ -459,3 +459,36 @@ Evaluate `code` in page. Send `args..` to the web page via `channel` in asynchronous message, the web page can handle it by listening to the `channel` event of `ipc` module. + +An example of sending messages from browser side to web pages: + +```javascript +// On browser side. +var window = null; +app.on('ready', function() { + window = new BrowserWindow({width: 800, height: 600}); + window.loadUrl('file://' + __dirname + '/index.html'); + window.on('did-finish-load', function() { + window.webContents.send('ping', 'whoooooooh!'); + }); +}); +``` + +```html +// index.html + + + + + +``` + +**Note:** +1. The IPC message handler in web pages do not have a `event` parameter, which + is different from the handlers on browser side. +2. There is no way to send synchronous messages from browser side to web pages, + because it would be very easy to cause dead locks. diff --git a/docs/api/ipc-browser.md b/docs/api/ipc-browser.md index 534a7a2..b7fe48d 100644 --- a/docs/api/ipc-browser.md +++ b/docs/api/ipc-browser.md @@ -7,6 +7,9 @@ is the `channel` when sending message. To reply a synchronous message, you need to set `event.returnValue`, to send an asynchronous back to the sender, you can use `event.sender.send(...)`. +It's also possible to send messages from browser side to web pages, see +[WebContents.send](browser-window.md#webcontentssendchannel-args) for more. + An example of sending and handling messages: ```javascript @@ -34,12 +37,12 @@ ipc.on('asynchronous-reply', function(arg) { ipc.send('asynchronous-message', 'ping'); ``` -### Class: Event +## Class: Event -## Event.returnValue +### Event.returnValue Assign to this to return an value to synchronous messages. -## Event.sender +### Event.sender The `WebContents` of the web page that has sent the message. -- 2.7.4