4

hi everyone i need to pass an array value to a new html page. i tried but the array value is always resetting

here is my code

var cart=[];
var jumlah=0;
var total=0;


function add(index)
{
if(index==1)
{
    jumlah=prompt("masukan jumlah");
    if(jumlah==0||jumlah<0||jumlah==null)
    {
        alert("harap masukkan jumlah, dengan format yang benar");
    }
    else
    {
    total=jumlah*500000;
    cart.push('Razer Destructor Battlefield @ IDR 500.000|| Jumlah '+jumlah+' || total '+total);
    for(var i=0;i<cart.length;i++)
    {
        alert("barang telah di tambahkan ke keranjang");
    }
    }
}
else if(index==2)
{
    jumlah=prompt("masukan jumlah");
    if(jumlah==0||jumlah<0||jumlah==null)
    {
        alert("harap masukkan jumlah dengan format yang benar");
    }
    else{
    total=jumlah*2020000;
    cart.push('Razer blackwidow ultimate mechanical keyboard for gaming @ IDR 2,020,000|| Jumlah '+jumlah+' || total '+total);
    alert("barang telah di tambahkan ke keranjang");
    }
}}

i need to pass that cart[] array from product.html to a a new page called cart.html

can u guys help me? sorry im a beginner and sorry for bad english. thanks for ur help!!

3
  • Store your cart array using localStorage/sessionStorage and then access it on the next page. Reference Commented Jan 10, 2014 at 6:27
  • its not possible to transfer content between HTML pages instead use Web Storage dev.w3.org/html5/webstorage Commented Jan 10, 2014 at 6:27
  • do it in server side instead of using javascript Commented Jan 10, 2014 at 6:31

2 Answers 2

10

You should have a look to localStorage

window.localStorage.setItem("cart", JSON.stringify(cart)); // Saving
var cart = JSON.parse(window.localStorage.getItem("cart")); // Retrieving
Sign up to request clarification or add additional context in comments.

2 Comments

thank neovov!!but am i need a different javascript on my cart.html page?
No, just use localStorage.setItem to save your cart in the browser, and localStorage.getItem to retrieve your data.
1

If you're supporting recent browsers, to save do:

localStorage.setItem('cart', JSON.stringify(cart));

To retrieve it from other html page in the same domain:

var cart = JSON.parse(localStorage.getItem('cart'));

If you have to support old browsers, apply the same logic but saving in cookies.

Cheers

2 Comments

thank Edgar!!but am i need a different javascript on my cart.html page?
Yes, you can, but first you have to remove the line var cart = [];

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.