Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

manipulating variable in different file

how can i manipulate variable in a different .js file, and making sure that the variable has changed and stays changed on that file even when the app terminates. example:

<exterior file> var number = 0; function increase_number () { number++}

<main file> increase_number();

CALLING THE METHOD THROUGH MY MAIN FILE DOESNT SEEM TO MAKE "number" EQUAL "1" WHEN I ENTER IT THROUGH MY TEXT EDITOR.

2 Answers

Dave McFarland
STAFF
Dave McFarland
Treehouse Teacher

You will need a way to save that variable to a server (with a database) or to a person's browser (with a cookie, or localstorage.) If the value is specific to one person -- in other words the number variable is different for each person visiting your site -- then using a cookie or local storage is a lot easier. You can check out this to learn about local storage options: http://www.html5rocks.com/en/tutorials/offline/storage/

If the variable is changing for EVERYONE who visits the site -- in other words everyone needs to get the same value -- then you need to use a database solution. Using PHP and MySQL is one solution http://teamtreehouse.com/library/using-php-with-mysql . Rails is another option for server-side database work http://teamtreehouse.com/tracks/rails-development

If you are physically trying to change the value of the variable i believe you will have to write directly to the file to change the code.

I would suggest maybe storing the value of the variable in a file in maybe JSON format and using javascript to pull that value in.