1

I want to access a common variable from two differnrt jquery scripts (which will be loaded externally to the same page). so I use

window.commonVar = 0;

My question is what are the options I've got to update it? So that both scripts have the same last updated value?

So basically if script 1 do commonVar++ , script 2 should know the current value. same way around.

Fergoso
  • 1,584
  • 3
  • 21
  • 44
  • 1
    Declare in master page – Amit Mar 18 '15 at 13:08
  • Simply Use window.commonVar to set or get the Variable. any change in this variable will be reflected to other scripts instantly – Puneet Mar 18 '15 at 13:09
  • @Amit: thanks. declare what on master page? the global variable? – Fergoso Mar 18 '15 at 13:10
  • 1
    It should [just work](http://plnkr.co/edit/vYUOK5mAJshyefhz8hkd?p=preview). Otherwise, perhaps the [observer pattern](http://en.wikipedia.org/wiki/Observer_pattern) is what you're looking for – Tom Mar 18 '15 at 13:10
  • This might be useful: http://stackoverflow.com/questions/9773964/understanding-the-javascript-global-namespace-and-closures – Michael Jaros Mar 18 '15 at 13:11
  • what does `know the current value` actually mean to you? Changing a variable value won't trigger an event to run code – charlietfl Mar 18 '15 at 13:11
  • 1
    Umm... i guess you would like to work on `localstorage`. – Jai Mar 18 '15 at 13:12
  • @Tom: thanks. Looks good. Perhaps soomething else is wrong in my script(s). I'll give it a try. – Fergoso Mar 18 '15 at 13:13
  • What is the actual problem you are having? – charlietfl Mar 18 '15 at 13:15
  • @Fergoso, I updated the link to a [plnkr](http://plnkr.co/edit/vYUOK5mAJshyefhz8hkd?p=preview) which actually shows using two separate scripts calling the same `window.commonVar` item. – Tom Mar 18 '15 at 13:15
  • @Tom: thanks you deserve 15. post and I'll accept. Now I've got the answer to this perticular question by narrowing down my problem. – Fergoso Mar 18 '15 at 13:20
  • @charlietfl: thanks. One of the scripts is not picking the global variable's updated value. Most probably I've made some mistake in my code. I'm going thru it. Hopefully I'll be able to locate it. thanks. – Fergoso Mar 18 '15 at 13:22
  • Then this is really an X-Y problem , you should be posting the code that doesn't work rather than asking the question you asked as the current question is masking your issue – charlietfl Mar 18 '15 at 13:27
  • @charlietfl: As per my question, I really was not sure how the global variable works and whether there are any conditions when using a global variable. So I decided to clarify my problem before asking someone else to write the code for me. I'd like to use this great stakoverflow forum to learn something. :) – Fergoso Mar 18 '15 at 13:31

1 Answers1

1

You have multiple options:

  1. Declare the variable in which ever script you load first.
  2. Declare the variable in your main html page in a script tag before you load your other scripts
  3. Declare the variable in a third script that you load first before the other two scripts. This script could be composed of all gobal variables that you need access via other scripts.
  4. You could even store it in a hidden input's value.
kemicofa ghost
  • 16,349
  • 8
  • 82
  • 131