简体   繁体   中英

passing variables from a script in the head to a script in the body

I have created an HTML document that contains several Javascript scripts. I have created in the head section of the page a Javascript function that generates a specific value. I would like to pass this variable in a Javascript block located in the body section. Is it possible? Thanks!

Yes, it is possible. Just make the function in the head global, and call it from the body script. Or call it in the head script and assign the result to a global variable that you access below.

Your question is not very specific. But this does exactly what you described and it works.

<html>
<head>
<script>var test = 'someVar';</script>
</head>
<body>

<script>

alert (test);
</script>
</body>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM