0
How to use a javascript variable in a script ? (But the variable was settled in another script on the same HTML page)
EXAMPLE <script> var x... </script> ...HTML codes .... <script> alert(x); </script>
3 Answers
+ 1
As long as you are accessing the variable "after" it is defined (as is the case in your example), and in the same page, it does not matter whether or not the variable is defined within the same <script> or a different one "anywhere" in the page.
0
but that code says that variable x is not defined (in the second script)
0
It's hard to tell what's causing it without seeing your code. Perhaps there is a JavaScript error that's preventing the execution of any subsequent code. Please check your browser console (developer tools) to see if there are any errors, and if yes fix them (or post it if you need help), and try again.