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?

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

3 Comments

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.
@JesseB what approach do you recommend?
Here's another SO question regarding globals: stackoverflow.com/questions/5072667/… 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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.