0

I am running a small piece of code to understand how browserify works. The code is using the json-rpc2 module. I have a file test.js

var rpc = require('json-rpc2')
var client = rpc.Client.$create(8545, 'localhost');
client.call('eth_coinbase', [], function(err, result) {
    console.log(result);
}

If I run node test.js like normal the code runs fine and the output is correctly written to the terminal. However, if I change console.log(result) to document.write(result), browserify it to a file bundle.js and include it in an HTML file when I run the web page nothing is written to the screen.

Does anyone know what I'm doing wrong? I feel like this is a no-brainer, but I can't figure it out.

jojeyh
  • 276
  • 1
  • 3
  • 12
  • Probably `document` is not defined. Try `console.log(document);` and check the result. – VTodorov Dec 14 '17 at 14:27
  • Isn't `document` defined when a web page is opened in Chromium? – jojeyh Dec 14 '17 at 14:30
  • It returned 'document is not defined' , how do I define document then? – jojeyh Dec 14 '17 at 14:32
  • 1
    If you are using jQuery you can try `$(document).ready()`, if you want a pure javascript solution check [this](https://stackoverflow.com/questions/9899372/pure-javascript-equivalent-of-jquerys-ready-how-to-call-a-function-when-t) – VTodorov Dec 14 '17 at 14:35
  • There are instances (like in an addon) that a JS can run before DOM is created. –  Dec 14 '17 at 14:40
  • Why you need to use `document.write`? Seems to be a little bit strange. – Frogmouth Dec 14 '17 at 14:41
  • @Frogmouth I'm just using it for testing purposes, if there's an easier way I'm all ears. – jojeyh Dec 14 '17 at 14:43
  • In general I avoid `document.write` because of its habit of clearing the document if called after the document has loaded (which *may* be happening to you). It also has some weird edge cases with async scripts and iframes. –  Dec 14 '17 at 14:54
  • @jojeyh - when You told: "It returned 'document is not defined' , how do I define document then?" you see this debug information from Browser console or in terminal? – Frogmouth Dec 14 '17 at 15:01
  • @Frogmouth I do not see anything in the browser console when I run `console.log(document)`. – jojeyh Dec 14 '17 at 19:50

0 Answers0