简体   繁体   English

如何将变量从 html 文件传递​​到 javascript 文件

[英]how to pass a variable from html file to javascript file

I need to pass a variable from page1.html file to page2.js .我需要将一个变量从page1.html文件传递给page2.js Must include pure javascript code (without tags like <script> ).必须包含纯 javascript 代码(没有像<script>这样的标签)。

page1.html : page1.html :

<script type="text/javascript">
  var age = 23;
</script>
<html>
  <!-- some code I need -->
</html>

page2.js : page2.js

print(age);
<script type="text/javascript">
  var age = 23;
</script>
<script type="text/javascript" src="your-second_script name"></script>
<html>
  <!-- some code I need -->
</html>

It should work Or else load both javascript externally and load first script which have that variable and then second script which is using that variable它应该可以工作,否则在外部加载javascript并加载具有该变量的第一个脚本,然后加载使用该变量的第二个脚本

You can add this code in your page1.html :您可以在page1.html添加此代码:

<input value="23" type="text" hidden="hidden" id="myVariable" />

and get your variable value in page2.js by this code:并通过以下代码在page2.js获取变量值:

alert(document.getElementById('myVariable').value)

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

相关问题 如何将变量从 HTML 文件传递到 JavaScript 文件 - How to pass variable from an HTML file to a JavaScript file 如何将Javascript变量传递到HTML文件 - How to pass a Javascript variable into a HTML file 将Flask HTML Template中的变量传递给JavaScript文件以供使用 - Pass variable from Flask HTML Template to JavaScript file for use 如何将 Object 值从 Javascript 文件传递到 HTML 文件 - How to Pass Object Value From A Javascript File to HTML File 将 HTML 文件内容传递给 JavaScript 变量 - Pass HTML File content to JavaScript variable 如何将变量从HTML脚本文件传递到Google Apps脚本中的javascript函数? - How can I pass a variable from an HTML script file to a javascript function in google apps script? 如何将元素值变量从 Javascript html 文件传递到 google apps 脚本中的 code.gs 文件? - How to pass an element-value variable from Javascript html file to code.gs file in google apps script? 如何将变量从html表单传递到.js文件? - How to pass a variable from a html form to a .js file? 如何将变量从 javascript 函数传递到 php 文件而不重新加载编写 javascript 函数的当前 html 页面? - how to pass a variable from javascript function to php file without reloading the current html page in which javascript function is written? 如何将数组变量从javascript传递到pug文件? - How to pass an array variable from javascript to a pug file?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM