0

I need the community to help me with the following:

I defined the variable x=1 in my js file. I have 2 HTML files that use that variable (1.html and 2.html). I want to use onclick event in 1.html to change the value of variable x to 2 permanently.. so that if I use x variable in 2.html it's value is 2 not 1.

This is what I have in java.js file:

x=1;

This is in 1.html:

<html>
    <head>
        <script type="text/javascript" src="java.js">
        </script>
    </head>
    <body>
        <input type="button" value="Change x" onClick="x=4">
            <p id="iz"></p>
    </body>
</html>

This is in 2.html:

<html>
    <head>
        <script type="text/javascript" src="java.js">
        </script>
    </head>
    <body>
        <input type="button" value="Change x" onClick="x=x+1">
            <p id="iz"></p>
    </body>
</html>

The result of the button in 2.html should be 5.

3
  • The only way to do this would be to pass the values from page to page, which is best done using sessions when x is changed in one page, so that it can be persisted between pages. You could also use local storage or cookies and load 'x' from those values if present and fallback to its value of '1' if it's not present. Commented Mar 1, 2012 at 22:27
  • Can you please example the code that I can use to persist the value between pages? I am using a frameset with 2 cols.. meaning I always have 2 html files open. If this makes things easier to understand I shall go into detail: I am trying to realise a game. For this my index is split in two frames, 1 being the map where you can click on buildings, the other being used for building properties and actions. Any suggestions? Commented Mar 2, 2012 at 22:40
  • I'd suggest, for now, then, learning specifically about cookies (perhaps using this: code.google.com/p/cookies) as a first-step. Commented Mar 2, 2012 at 22:47

6 Answers 6

2

JavaScript doesn't work like that. There's no persistence between pages without using cookies, or passing the state to the server.

If you navigate to foo.html and it sets var foo = 1 and then navigate to bar.html, foo will not have been set.

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

Comments

0

Short answer, you can't only using JavaScript - the web is stateless.

Though, can use a cookie or local storage as your backing store to hold the value. Initially setting it to 1, any subsequent modifications effect the backing store and reflect the proper value.

Comments

0

In java script there is no persistence of variable as you want, you have to use cookies, a server side aproach or the local storaged offered in html5

Comments

0

This is not normal behaviour for JavaScript because the values you set will on persist for the duration of either page. What you probably want to do is store the value of x on a server, store it in a cookie, or store it in localStorage.

Comments

0

In each of your pages it is pulling in x=1. Just because you change a variable on one page doesn't mean it is going to be seen on another page. This is where you need to pass variables along to the other pages with either GET or POST or cookies or local storage

1 Comment

I will try this first as I do not yet know how to use cookies.
0

Your input tags are not closed, that can't be right. Fix these before looking for other reasons for bugs in your code:

<input type="button" value="Change x" onClick="x=4">

Should perhaps be:

<input type="button" value="Change x" onClick="x=4"/>

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.