📝 Say more on sending messages from browser to web page.
This commit is contained in:
parent
a67c54869f
commit
f25480dec5
2 changed files with 39 additions and 3 deletions
|
@ -459,3 +459,36 @@ Evaluate `code` in page.
|
||||||
|
|
||||||
Send `args..` to the web page via `channel` in asynchronous message, the web
|
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.
|
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
|
||||||
|
<html>
|
||||||
|
<body>
|
||||||
|
<script>
|
||||||
|
require('ipc').on('ping', function(message) {
|
||||||
|
console.log(message); // Prints "whoooooooh!"
|
||||||
|
});
|
||||||
|
</script>
|
||||||
|
</body>
|
||||||
|
</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.
|
||||||
|
|
|
@ -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
|
to set `event.returnValue`, to send an asynchronous back to the sender, you can
|
||||||
use `event.sender.send(...)`.
|
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:
|
An example of sending and handling messages:
|
||||||
|
|
||||||
```javascript
|
```javascript
|
||||||
|
@ -34,12 +37,12 @@ ipc.on('asynchronous-reply', function(arg) {
|
||||||
ipc.send('asynchronous-message', 'ping');
|
ipc.send('asynchronous-message', 'ping');
|
||||||
```
|
```
|
||||||
|
|
||||||
### Class: Event
|
## Class: Event
|
||||||
|
|
||||||
## Event.returnValue
|
### Event.returnValue
|
||||||
|
|
||||||
Assign to this to return an value to synchronous messages.
|
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.
|
The `WebContents` of the web page that has sent the message.
|
||||||
|
|
Loading…
Reference in a new issue