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>

21st Jun 2017, 12:46 AM
Bruno Kappi
Bruno Kappi - avatar
3 Antworten
+ 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.
21st Jun 2017, 1:49 AM
Alex
0
but that code says that variable x is not defined (in the second script)
21st Jun 2017, 1:51 AM
Bruno Kappi
Bruno Kappi - avatar
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.
21st Jun 2017, 2:07 AM
Alex