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