1

Can anyone tell me why nothing is being output on my page when trying to run this code?

I am trying to parse the localStorage item and create a table for each item by using a for loop. I am then adding the total price.

Note: localStorage contains 3 items and is NOT empty.

<!DOCTYPE html>
<html>
<head>
    <meta content="text/html;charset=utf-8" http-equiv="Content-Type">
    <meta content="utf-8" http-equiv="encoding">
    <style>img{ height: 100px; float: left; }</style>
    <link rel="stylesheet" type="text/css" href="style.css">

    <script src="http://code.jquery.com/jquery-1.7.1.min.js"></script>


    <script>

        function ShoppingCart() {

            var totalPrice = 0;
            var output;

        for (var i = 0; i < localStorage.length; i++){

           var product =  localStorage.getItem('Product_'+i);

            var result = JSON.parse(product);

            var productName;
            var productAlbum;
            var productQuantity;
            var productPrice;
            var productSubTotal = 0;
            var totalPrice;

           productName = result.name
           productAlbum = result.album;
           productQuantity = result.quantity;
           productPrice = parseFloat(result.price);
           productSubTotal = productQuantity * productPrice;

           totalPrice = totalPrice + productSubTotal;


            outputName = "<div id='cart-table'><table><tr><td>NAME:" + productName + "</td></tr></div>" ;
            outputAlbum = "<tr><td>ALBUM:" + productAlbum + "</td></tr>" ;
            outputQuantity = "<tr><td>QUANTITY:" + productQuantity + "</td></tr>";
            outputPrice = "<tr><td>PRICE:" + productPrice + "</td></tr>";
            outputSubTotal = "<tr><td>SUB-TOTAL" + productSubTotal + "</td></tr></table><br><br>";




                        }

            var outputTotal = "<table><tr><td>" + totalPrice + "</td></tr></table>";
            var TotalOutput = outputName + outputAlbum + outputQuantity + outputPrice + outputSubTotal + outputTotal;
            document.getElementById("Cart-Contents").innerHTML=TotalOutput

                    alert(TotalOutput);
        }

          window.onload = ShoppingCart();


    </script>


</head>
<body>
<div id="wrapper">

    <header id="header">
        <strong><table id="menu"><tr><td>Home</td><td>Contact</td><td>Login</td><td></td><td>Products</td></tr></table></strong>
    </header>
    <section id="middle">

        <div id="container">
            <div id="content">
                <a class=""  onclick="ShoppingCart()"><span>Cart</span></a>
                <div id="Cart-Contents">


            </div>


            </div><!-- #content-->
        </div><!-- #container-->

        <aside id="sideLeft">
            <strong>Left Sidebar:</strong>
        </aside><!-- #sideLeft -->

        <aside id="sideRight">
            <strong>Right Sidebar:</strong>
        </aside><!-- #sideRight -->

    </section><!-- #middle-->

</div><!-- #wrapper -->



<footer id="footer">
    <strong>by Brian Livori</strong>
</footer><!-- #footer -->
</body>
</html>
1
  • Are you sure the items in localStorage start with "Product_"? Depending on how you set the items, it might not have a length. Either way, you might want to loop with a for in loop Commented Apr 25, 2013 at 1:05

1 Answer 1

2

It is simple, just change

window.onload = ShoppingCart();

to

window.onload = ShoppingCart;

window.onload is a callback function reference. When you assign ShoppingCart() - you actually assign the result of function execution to window.onload instead.

Example here http://jsbin.com/urikub/1/edit

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

5 Comments

Explain why. It may be obvious to us what's happening, but not to the OP.
@Vadim Nope, I still get no output. Page remains blank!
Ahh it worked, however the Name and Albums are not getting shown! Any help ?
@Vadim for some reason, the localStorage is not always getting apssed from one page to the next. any idea why?
@Brian localStorage uses same origin policy, that means that if schema, zone, domain (including subdomains) or port of the URL have changed - you will not get the same localStorage, but new one. Check that your origin does not change when you go from page to page.

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.