[英]How to display value from a Javascript function onto an HTML page?
index.js file index.js文件
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 ** ** index.html文件**
<!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 所以我正在使用以下代码片段中的值显示在我的HTML页面中
var output = document.getElementById("output");
output.innerHTML = "hello world"
Through this on my HTML page: 通过此操作在我的HTML页面上:
<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.
只是试图显示一些数据从我的javascript页面到html页面。
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. 在操作之前,请检查DOM是否准备就绪。
The name of your JavaScript file is index.js. 您的JavaScript文件的名称是index.js。 But you are including script.js file in the script tag.
但是您要在script标签中包含script.js文件。 Change the script tag to specify path of index.js file:
更改脚本标记以指定index.js文件的路径:
<script src="./path_of_file/index.js"></script>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.