1

I'm working on a project with C# in ASP.NET and using the JavaScript Library Raphael.

I have created some circles that I want to have as global variables to be able to modify their position as certain events happen. where should I define the global variables?

also I have two different .js files with functions and want to call one function from a button

  OnClientClick="return doSomething();"

Do I need to mention the file name there?

NicoTek
  • 1,127
  • 1
  • 14
  • 34

1 Answers1

3

You can define your global var inside a tag but outside any function for example in the head section of the page ; in this way you get variables accessible from everywhere in your page.

Function name must be unique, for included js script too, for call a js function your example is right.

Irvin Dominin
  • 30,819
  • 9
  • 77
  • 111
  • 1
    This is correct. JavaScript is parsed in a top-down fashion (line-by-line). So declare your globals as the first js items in the header tag of the page (before other js files as well, at least being declared before their first use). I should mention true globals are considered bad practice in js. You should look into using another approach, as continuing with this approach will probably lead to confusing and hard to maintain code. – JesseBuesking Nov 20 '11 at 21:44
  • @JesseB what approach do you recommend? – NicoTek Nov 20 '11 at 22:12
  • Here's another SO question regarding globals: http://stackoverflow.com/questions/5072667/javascript-pattern-to-avoid-global-variables Basically just create your own namespace for your methods/variables. That way if you expand the project down another venue that is similar, and you find yourself wanting to use variables/functions with names that already used, your namespace will protect you from any confusion. – JesseBuesking Nov 21 '11 at 05:59