-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbrowsercode.js
More file actions
38 lines (29 loc) · 923 Bytes
/
browsercode.js
File metadata and controls
38 lines (29 loc) · 923 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
// lets do a fetch connection using the fetch API
fetch('/urltoget').then(response=>{
response.text().then(value=>{
console.log('Value of the response: '+value)
//document.body.innerHTML = value.replace(/Google/g,'Frooble')
})
})
// websocket example
document.body.innerHTML =
"<input id='text' type='text'/>\n"+
"<input id='send' type='button' value='send'/>"
var textElement = document.getElementById('text')
var sendElement = document.getElementById('send')
function send(){
websocket.send(textElement.value)
textElement.value = ''
}
textElement.addEventListener('keyup', event=>{
if(event.keyCode===13) send()
})
sendElement.addEventListener('click', send)
var websocket = new WebSocket('ws://'+location.host+'/websocket')
websocket.onopen = event=>{
}
websocket.onmessage = event=>{
div = document.createElement('div')
div.innerHTML = event.data
document.body.insertBefore(div, textElement)
}