简体   繁体   English

如何将Javascript函数的值显示在HTML页面上?

[英]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.

相关问题 如何显示从Javascript函数到HTML的计算? - How to display calculations from Javascript function onto HTML? HTML JavaScript 从文件打印到页面上 - HTML JavaScript print from file onto page 如何使用Javascript将另一个网页上的文本显示到另一个页面上? - How can I use Javascript to display text from another web page onto a different page? 如何将所选日期选择器值从第一页显示到第二页日期选择器 - How to display the selected datepicker value from the 1st page onto the 2nd page datepicker 如何在解析html页面时从html页面中的javascript函数中提取变量的值 - how to extract a value of variable from a javascript function in the html page while parsing an html page 如何将文本区域注释框中的信息显示到当前 html 页面上 - How to display the information from a textarea comment box onto the current html page 我如何将Http获取请求文件中的Json数据显示到空白html页面上 - How would I display a Json data from my Http get request file onto a blank html page 如何在JSON页面上显示JSON对象列表 - How to display a list of JSON objects onto html page Javascript从文本文件中读取值并显示在html页面中 - Javascript to read value from a text file and display in html page 将[Map,]中的[key,value]显示到html表中 - Display [key, value] from `Map` onto html table
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM