Paul's Programming Notes     Archive     Feed     Github

Global Variables In Javascript

Problem: I had two different <script> (javascript) sections, and I was unable to pass a variable from one set of <script> tags to the other.

Turns out it doesn't have anything to do with the script tags, and I just needed to learn a bit more about scope in Javascript. Apparently global variables are a common thing (maybe still not a good practice?) and as long as a variable is used outside of a function.. You'll be able to access the variable anywhere on the page.

Solution: Take the variable outside of the function.