1

I'm trying to overwrite my variables with the ones stored on another .js file. Let's say that i have set these values:

var image1 = '1.jpg'; var image2 = '2.jpg'; var image = '3.jpg';

And that i have another file only with variables:

var image1 = 'john.jpg'; var image3 = 'mary.jpg';

So, I want these values after the file is loaded:

var image1 = 'john.jpg'; var image2 = '2.jpg'; var image3 = 'mary.jpg';

There's a way to do that?

  • 1
    When the browser loads your javascript it doesn't keep a concept of individual files. It loads everything into memory so the last defined value is in scope. It sounds like you need to rethink what you are doing and read up a bit more on how JS works. – scrappedcola Apr 01 '15 at 20:30
  • I think this solution 'might' be what you're looking for: http://stackoverflow.com/questions/2932782/global-variables-in-javascript-across-multiple-files – Dan Beaulieu Apr 01 '15 at 20:34
  • You could store your variables in a global object. The simplest way would be to put it on the window object.. But I do not recommend this. Other ways would be to look into frameworks that do these types of things.. IE (Angular, Backbone, jQuery Widgets). Just some ideas... This is assuming you are working with a Single page application.. Otherwise you might want to look into session storage. Combined with a global object. – khollenbeck Apr 01 '15 at 20:40

0 Answers0