4

I am using Javascript and Jquery to code for a mobile app for my school assignment. I am trying to transfer the value of variable days to another page by using the get function, the codes below are the things I've done so far. What I am trying to do here is to assign the variable an id on result.html so that i can call it on schedule.html. However, the variable value is not appearing in schedule.html when 'write.document-ed', with the result instead being [Object object].

result.html

<html>
var days = (weight/0.064); 
var days= document.getElementById('myday').value;
</html>

schedule.html

<html>
var days;
var days = document.getElementById('myday').value;
write.document(day);
</html>

The project has 4 files, three htmls and one js file. First html file, index.html, receives inputs from forms and place them in functions and submits to result.html. Common.js is shared with all the html files, to put in all the different formulas and functions in.

I am aware how to transfer variables inside common.js into the pages, as I did with index.html to result.html using forms, using this method:

index.html

 function bigfunction(){
    var weight = 
    smallfunction(parseFloat(document.form.Weight.value));
    document.form.weight.value = weight;
    document.form.submit()

Common.js

smallerfunction(weight){
var weight = weight;
}

result.html

<script>
var weight;
 weight = decodeURIComponent(getUrlVars()["weight"]);
</script>

Weight = <script>document.write(weight)</script>

However, I do not think I can use the same thing I did above to transfer variable value from result.html to schedule.html, as there is no forms on result.html to submit to next page. I have Jquery and all the software installed nicely, I think I heard something about using DOM to do this, but I am not sure how.. I think I would prefer an answer using get like I did in the first two code examples and DOM but as long as it works I am all ears!!!!

Answer: localstorage

result.html

var days = localStorage.setItem("days", days);

schedule.html

var days;
days = localStorage.getItem("days");
days: document.write(days)

thank you alll !!!!!!!!!!!!!!!!!!!!!!!!!!

5
  • 4
    Local storage, query or hash in the URL, window.postMessage(), database in the backend, a cookie. So many options, pick your favourite one and Read Up. Submitting forms is normally meant to send data to the back end server, not another front end page. getElementById only has access to its own page, not other pages. What does your textbook say? Commented Nov 25, 2015 at 15:12
  • my textbook has two methods, local storage and getUrlvars, the one I used before. I guess i'll try localstorage after trying out the query code !! Commented Nov 25, 2015 at 15:19
  • mann I am having the same problem again, the codes my textbook has gets their value from forms and submits the form in the end to 'run' the function, but I have no forms to submit for. localStorage.setItem("hoursworked", parseFloat(document.wageform.hoursworked.value)); document.wageform.submit(); wait lemme try something Commented Nov 25, 2015 at 15:28
  • babyyyyyy!!!!! it works!!!! you got me out of the time sink hole !!!!!!!! thank you !!!!!! what i did : result.html : localstorage.setitem("name", days), schedule.html var days; localsstorage.getitem("days"); Commented Nov 25, 2015 at 15:43
  • I'm glad you solved your problem. Please create an answer instead of answering in your question please. Commented Nov 25, 2015 at 16:21

2 Answers 2

2

There are more than just two ways, but I would recommend the second one here:

The more persistent way (data stays even if the browser gets closed), is shared between tabs (browser scope):

// setter
localStorage.setItem("data", { date: 'whatever', anotherKey: 'somevalue' });
// getter
var data = localStorage.getItem("data");

Or, what I would prefer for non-persistent data, sessionStorage (stays until the browser/tab gets closed), not shared between tabs (tab scope)

// setter
sessionStorage.setItem("data", { date: 'whatever', anotherKey: 'somevalue' });
// getter
var data = sessionStorage.getItem("data");

Be aware that using localStorage might lead to unwanted behavior if some data stays there and will be presented the user again if he comes back some days/weeks later.

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

Comments

0

Answer: localstorage

result.html

var days = localStorage.setItem("days", days);

schedule.html

var days;
days = localStorage.getItem("days");
days: document.write(days)

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.