简体   繁体   English

如何从 html 文件中获取保存的数据?

[英]How to get saved data from html file?

I have some data saved inside the html file, which looks like this:我在 html 文件中保存了一些数据,如下所示:

<head>
   <script type="text/javascript" src="app.js"></script>
</head>

<body>

  <script type="text/javascript">
  document.__data = {
     id: 1,
     text: 'Test blob',
  };
  </script>
</body>

the data is saved locally inside the data.html file.数据本地保存在data.html文件中。
Now, I'm trying to load the data into a separate app.js file:现在,我正在尝试将数据加载到单独的app.js文件中:

I initially tried accessing the document directly:我最初尝试直接访问document

const getData = () => console.log(document.__data);

But that jsut returns as undefined so then I tried the following:但是那个 jsut 返回undefined ,所以我尝试了以下操作:

 // app.js
 const getData = async () => {
     const request = await fetch('./data.html');
     const data = await request.json();
     console.log(JSON.stringify(data));
 }

 getData();

But that just returns the following error:但这只会返回以下错误:

Uncaught (in promise) SyntaxError: Unexpected token < in JSON at position 0 Uncaught (in promise) SyntaxError: Unexpected token < in JSON at position 0

Any idea how to go about this?知道如何 go 关于这个吗?

First off, adding custom, arbitrary properties to the document is not recommended, as in:首先,不建议向document添加自定义的任意属性,如下所示:

document.__data = { ... };

instead do something like eg而是做类似的事情

var data = { ... };

Now, assuming you want to access the inline data using an external app.js file, that file needs to be loaded after your inline one, or else it will come back undefined .现在,假设您想使用外部app.js文件访问内联数据,该文件需要在您的内联文件之后加载,否则它将返回undefined

So this is how it needs to look like in your HTML:这就是它在 HTML 中需要的样子:

<head>
</head>

<body>

  <script>
    var data = {
      id: 1,
      text: 'Test blob',
   };
  </script>

  <script src="app.js"></script>

</body>

And your app.js还有你的app.js

const getData = () => console.log(data);

getData();

If your external app.js loads within the head , as in your original HTML, you need to delay the getData() until the DOM is ready, and this is how it needs to look like in your app.js :如果你的外部app.jshead中加载,就像你原来的 HTML 一样,你需要延迟getData()直到 DOM 准备好,这就是它在你的app.js中的样子:

const getData = () => console.log(data);

window.addEventListener('DOMContentLoaded', (event) => {

    getData();

});

And the reason is simply that you can't access what comes later in the document as it is not loaded yet, hence the undefined issue in your first sample.原因很简单,您无法访问文档中稍后出现的内容,因为它尚未加载,因此在您的第一个示例中存在undefined的问题。


Below two snippets use inline script so you can see how it works.下面的两个片段使用内联脚本,因此您可以看到它是如何工作的。 Just replace the getData() part with your app.js and it will work as well.只需将getData()部分替换为您的app.js

Showing it does work when DOMContentLoaded is finished.DOMContentLoaded完成时显示它确实有效。

 <head> <script> const getData = () => console.log(data); window.addEventListener('DOMContentLoaded', (event) => { getData(); }); </script> </head> <body> <script> var data = { id: 1, text: 'Test blob', }; </script> </body>

Showing it does not work when not delayed until DOM is finished在 DOM 完成之前不延迟显示它不起作用

 <head> <script> const getData = () => console.log(data); getData(); </script> </head> <body> <script> var data = { id: 1, text: 'Test blob', }; </script> </body>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 HTML表单中的数据未保存在我的javascript函数中 - Data from HTML form does not get saved in my javascript function 如何使用保存在另一个html文件中的数组中的表单数据 - how to use form data saved in an array in another html file 如何从 PHP 文件中获取 JSON 数据以在 html 文件中打印 - How to get JSON data from a PHP file to print in html file 如何从 mp3 文件中获取数据并自动将其放在 html 上? - how to get data from an mp3 file and put it on html automatically? 使用javascript或jquery从保存的html中获取与元素相关的jquery.data() - get jquery.data() related to an element from a saved html using javascript or jquery 如何从另一个HTML文件中的一个HTML Apps脚本文件获取JSON数据? - How do I get JSON data from one HTML Apps Script file in another HTML file? 如何使用保存的html从Gridstack构建网格 - How to build grid from Gridstack with saved html 我想从 html 表单存储数据。 但我想将其保存到本地文件(文本或 csv 文件 - 用于分类 - ) - I want to storage data from html forms. But I want to make it saved to a local file (a text or csv file - for categorization - ) 如何使用 javascript 从不同的文件夹文件和 html 文件名中获取数据 - how to Get data from different folder file and that html file name using javascript 如何获取从html文件发送到php的数据并接收并显示数据 - How can I get the data that is sent from my html file to php and recieve and show the data
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM