2 Replies - 505 Views - Last Post: 02 August 2014 - 02:42 AM

#1 candicedgy  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 01-August 14

Is it possible to use the same array function 4 different HTML pages?

Posted 01 August 2014 - 04:52 AM

<html>
<body>
	<form name="try">
	<input type="radio" name="lala" onclick="myFunction2()">Try it</input><br>
	<input type="checkbox" name="peach" value="true" onclick="myFunction(this)">Try it</input><br>
	<input type="checkbox" name="peach" value="love" onclick="myFunction(this)">Try it</input><br>
	<input type="checkbox" value="happy" onclick="myFunction(this)">Try it</input><br>
	<input type="checkbox" value="ness" onclick="myFunction(this)">Try it</input><br>
	<textarea name="totalpriceB" cols="50" rows="5" id="totalpriceB"></textarea>
	<textarea name="count" cols="50" rows="5" id="count"></textarea>
	<input type="reset" name=resetbutton value=Reset>
	</form>

	<script>
		
		var ingredients = [];
		

		function myFunction(clicked_element) {
			text = clicked_element.value;
			if(clicked_element.checked) {
				ingredients.push(text);
				document.getElementById('totalpriceB').value = ingredients;
					if(clicked_element.checked) {
						var count = ingredients.length;
						document.getElementById("count").value = count;
						}
			}
			else {
				ingredients.pop(text);
				document.getElementById('totalpriceB').value = ingredients;
				}
		}
		
		function myFunction2(){
			document.try.peach[0].click();
			document.try.peach[1].click();
			}
	</script>

</body>
</html>



I have that so far for 1 html page, let's name that one html1. But what I want to do is when I use another page, html2. I want to continue with what arrays values i have so far from html 1. Same goes for html3 and html4.

I want to be able to push values to "var ingredients = [];" from all 4 html pages and I would like "var ingredients = [];" to collect them all so that I could use them for something else. So far I haven't had any luck finding answers. I already tried just copying the code to html2 but it's entirely a new array by then.

Is that possible?

Is This A Good Question/Topic? 0
  • +

Replies To: Is it possible to use the same array function 4 different HTML pages?

#2 ge∅  Icon User is offline

  • D.I.C Addict

Reputation: 80
  • View blog
  • Posts: 566
  • Joined: 21-November 13

Re: Is it possible to use the same array function 4 different HTML pages?

Posted 01 August 2014 - 05:53 AM

You can either use AJAX to load your different pages without actually reloading the page (and thus ever destroying the array), or use a cookie, a server session or localStorage to temporally store a stringified version of your array, then rebuild it in the next page.

I would advise the cookie solution (if the data is not private/sensible) : it's more compatible than localStorage and it doesn't require a server-side script.
Was This Post Helpful? 0
  • +
  • -

#3 baavgai  Icon User is online

  • Dreaming Coder
  • member icon

Reputation: 5849
  • View blog
  • Posts: 12,708
  • Joined: 16-October 07

Re: Is it possible to use the same array function 4 different HTML pages?

Posted 02 August 2014 - 02:42 AM

One solution is to persist the data on a server, as noted. However: do you really need four separate HTML pages?

If your web application is heavily Javascript driven, then you're dead in the water without Javascript, anyway. Embrace that and try a Single Page Application (SPA).

e.g.
<body>
<div id="page1">...</div>
<div id="page2" style="display:none">...</div>
<div id="page3" style="display:none">...</div>
<div id="page4" style="display:none">...</div>



I'd also consider using jQuery and binding all those clicky events in code.

Hope this helps.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1