2

I want to create a globally accessible variable. It must change when we want to change. After change it must give the changed value.

var language ="english";

function getGlobalVar(varName) {
    if(varName == "language"){
        return language;
    }
}

function setGlobalVar(varName, value) {
    if(varName == "language"){
       language = value;
    }
}

When we use getGlobalVar("language") in other pages, It must give the changed value.

4 Answers 4

5

A global variable is just a variable attached to window:

window.language = 'english';

function getGlobalVar(bar) {
  return window[bar];
}

It's recommended that you create your own namespace to avoid problems later on:

window.MY = {};
MY.language = 'english';
Sign up to request clarification or add additional context in comments.

2 Comments

I think this answer is work for my global variable problem. But using this we can't create a static variable across pages. Now I think create a static variable across pages in JavaScript is impossible.
You could use the history api and pass them in the URL or use localStorage.
2

When we use getGlobalVar("language") in other pages, It must give the changed value

The web uses a stateless protocol, i.e. information does not persist from page to page. To get around that, you can use cookies on the client side or server side solutions like sessions.

Try this:

1.htm

<script type="text/javascript">
    window.val = "hello world";
    alert(window.val);
    window.location.href = "2.htm"
</script>

2.htm

  <script type="text/javascript">
    alert(window.val);
  </script>

cookie example:

1.htm

<script type="text/javascript">
    var in_one_year = new Date();
    in_one_year.setFullYear(in_one_year.getFullYear() + 1);
    document.cookie = "language=English" +
                      ";expires=" + in_one_year.toGMTString();


    all_cookies = document.cookie
    alert(all_cookies);
    window.location.href = "2.htm"
</script>

2.htm

<script type="text/javascript">
    alert(document.cookie);
</script>

6 Comments

That means every time we refer the javascript file it gives new instance?
It has nothing to do with the js file. If you use js to attach a value to the window object of the web page, then when a new page is loaded, no information persists, i.e. the new page's window object will not have any values attached to it.
can't we create a static variable persists across pages in JavaScript file without attaching variable to the window object?
You are going to have to come to an understanding that the www uses a stateless protocol. Until you understand what that means, you cannot understand web programming.
Okay, I see what you mean by the js file. You can certainly create static variables for a javascript class. However, when a new page loads that page will need to load the js file to have access to the class. And your js file will load anew, and as far as that page is concerned that is the first time the class has ever been used and static variables will have their initial state.
|
0

Another solution for most browsers which support local storage.

JStorage - store data locally with JavaScript

Comments

0

With ES15, you can now simply have classes with static variables:

class ClassWithGlobalVariable {
  static language = 'en';
}

console.log(ClassWithGlobalVariable.language);
ClassWithGlobalVariable.language = 'cn';
console.log(ClassWithGlobalVariable.language);

Comments

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.