I have two pages - "page 1" and "page 2". On page 1 there is a text box with a value, for example. 100 and a button at the end.
By clicking the button, I want javascript to save the text field value in a global (?) Variable and go to page 2. Using "window.onload" I want the second Javascript function to display the value stored on page1.
Here is my Javascript code:
<script type="text/javascript"> var price; //declare outside the function = global variable ? function save_price(){ alert("started_1"); //just for information price = document.getElementById('the_id_of_the_textbox').value; alert(price); //just for information }
<script type="text/javascript"> function read_price(){ alert("started_2"); alert(price); }
On "page 1" I have this submit button with:
<input class="button_send" id="button_send" type="submit" value="Submit_price" onclick="save_price();"/>
It runs the Javascript function and redirects me correctly to my page.
But at the same time on the second page:
window.onload=read_price();
I always get the value "undefined" of the global price of a variable.
I read a lot about these global variables. For example. on this page: Problem with a global variable .. But I can't get it to work ...
Why is this not working?
javascript variables html undefined global
Kronwied
source share