Passing data in javascript

2

Next, how can I do to pass data between two files with javascript? Example:

File1:

<script>
var dado = 'dado';
</script>

File2:

<script>
alert (dado);
</script>
    
asked by anonymous 11.05.2015 / 23:28

1 answer

1

You have to consider 2 things:

  • Both are in the same scope (global or common)
  • the variable is defined before alert(); . In other words, this code loaded first.

If they are not in the same scope you can use a global object for this variable to be accessible:

File1:

<script>
window.dado = 'dado';
</script>

File2:

<script>
alert(window.dado);
</script>

If you are using different HTML pages, you can use the % a> and use localstorage instead of the example with localstorage I gave above.

Example where you write to the localstorage: link
Example where it reads from the localstorage: link

    
11.05.2015 / 23:32