简体   繁体   中英

How to display value from a Javascript function onto an HTML page?

index.js file

const {BrowserWindow, app, globalShortcut} = require('electron');
const url = require('url');
const si = require('systeminformation');



let win = null

function boot() {
//console.log(process.type)
win = new BrowserWindow({
    width: 600,
    height: 500,
    frame: false
})

var output = document.getElementById("output");
output.innerHTML = "hello world"
//win.loadURL(file:'//${__dirname}/index.html')
win.loadURL('file://' + __dirname + '/index.html');
win.on('closed', () => {
    win = null
})
}

app.on('ready', boot); 

**index.html file **

<!DOCTYPE html>
<html lang= "en">
<head>
<meta charset="UTF-8">
<title>Hello World</title>
<link rel="stylesheet" href="style.css">

</head>
<body>
<div id="content">
    <header>
        <div class="option" id="close">X</div>
        <div class="option" id="minimize">-</div>
    </header>
<div id="text">z</div>


 <h1>
  <p id="output"></p>
</h1>
</div>
<script src="./assets/js/script.js"></script>
</body>
</html>

So I am using the value from the following snippet to be display into my HTML page

var output = document.getElementById("output");
output.innerHTML = "hello world"

Through this on my HTML page:

<h1>
  <p id="output"></p>
</h1>

But it gives me the error:

"reference error :document is not defined "

By the way I am creating an Electron app. Just trying to display some data from my javascript page to html page.

As per the code you provided, you are referring to the document before it gets loaded.

var output = document.getElementById("output"); // <- here and
output.innerHTML = "hello world";
win.loadURL('file://' + __dirname + '/index.html'); // <- here

Check if the DOM is ready before manipulating it.

The name of your JavaScript file is index.js. But you are including script.js file in the script tag. Change the script tag to specify path of index.js file:

<script src="./path_of_file/index.js"></script>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM