Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <!DOCTYPE html>
- <html>
- <head>
- <title>test</title>
- <script type="text/javascript">
- var product = ["Pepsi","Coca","Sting","Piss","Canon in D", "Beethoven","Mozart sida","HIV","VIRUS ZIKA","FRIENDZONE","LOVERS","KHANH LINH","XNXX","VLXX","THIEN DIA HOI"];
- var price = [65,56,48,654,65,4,6,165,1,6500000,16,5100,560,160,501];// product and price
- var cart = {
- "avai_st":function(){
- return (typeof(Storage) !== "undefined");
- },
- "saved" : function(item){
- if(this.avai_st){
- localStorage.setItem("cart", item);
- }
- },
- "append":function(id){
- id = id.toString();
- var mount = parseInt(document.getElementsByName("product"+id)[0].value);
- document.getElementsByName("product"+id)[0].value = 0; // set back for product = 0;
- if(mount==0) return 0;
- if(this.avai_st){
- var fl =0;// use later
- var ob_item = new Object;
- ob_item[id] = mount;// create object {id:item}
- var store_str = localStorage.getItem("cart");
- if(store_str==null) {//check cart exist or not.
- this.saved(JSON.stringify([ob_item]));//not exist, create new
- }
- else{
- var json_arr = JSON.parse(store_str);// exist, convert to array object (array have object inside [{object},{object}, ...{object}])
- for(var i = 0; i< json_arr.length;i++){// search (is product in cart or not)
- if(json_arr[i][id]!=undefined){json_arr[i][id]+=mount;fl=1; break;}// product existed, sum more mount
- }
- if(fl!=1){
- json_arr.push(ob_item);// product not existed in cart, append product.
- };
- this.saved(JSON.stringify(json_arr));// save cart
- }
- alert("You added "+mount+" "+product[parseInt(id)]+" successfully!");
- }else{
- alert("Added fails! Restart your browser.");
- }
- },
- "get":function(){
- var item = "";
- if(this.avai_st)
- item = localStorage.getItem("cart");
- document.getElementById("bought").innerHTML = item;
- },
- "del_cart":function(){
- localStorage.clear();
- },
- "update":function(){
- if(this.avai_st){
- document.getElementById("cart").innerHTML = ""; // làm sạch cart trên html source
- var sum_money= 0;
- var store_str = localStorage.getItem("cart");
- if(store_str!=null){// kiểm tra nó còn sp hay k?
- var json_arr = JSON.parse(store_str);
- for(var i=0;i<json_arr.length;i++){
- var id = Object.keys(json_arr[i]);// lấy id của sản phẩm
- var mount = json_arr[i][id];// lấy số lượng
- var input="";
- input += "<img src=\"image\/"+id+".jpg\" width=\"40\" height=\"40\"><h4>"+product[parseInt(id)]+"<\/h4><br \/>";// hình của sp
- sum_money+=(mount*price[parseInt(id)]);// tổng tiền
- input += "<p>"+mount+" * $"+price[parseInt(id)]+" = $"+(mount*price[parseInt(id)])+"<\/p>";// tính tiền sản phẩm (price * với số lượng)
- input += "<hr \/>";
- document.getElementById("cart").innerHTML += input;
- }
- }
- document.getElementById("cart").innerHTML += "<h3>Sum = $"+sum_money+"</h3>";// in ra tổng tiền
- }
- }
- }
- function add_pro(item){
- document.getElementById("product").innerHTML += item;
- }
- function cha_m(state,id){
- var num = parseInt(document.getElementsByName("product"+id)[0].value);
- if(num==0 && state == 0) return 0;
- else if(state == 1) num+=1;
- else num -= 1;
- document.getElementsByName("product"+id)[0].value = num;
- }
- function products(){
- for(var i = 0; i <product.length; i++){
- add_pro("<li><img src=\"image/"+i+".jpg\" width=\"150\" height=\"150\"><h3>"+product[i]+"</h3><p>$"+price[i]+"</p><button onclick=\"cha_m(0,"+i+")\">-</button><input type=\"text\" value=\"0\" name=\"product"+i+"\" disabled><button onclick=\"cha_m(1,"+i+")\">+</button> <button onclick=\"cart.append("+i+")\">Add to cart</button></li>");
- if((i+1)%4==0) add_pro("<br />");
- }
- }
- </script>
- <style type="text/css">
- ul.products li {
- width: 200px;
- display: inline-block;
- vertical-align: top;
- text-align: center;
- border-style: solid;
- }
- ul.products p {
- color: red;
- }
- ul.products input {
- color: blue;
- text-align: center;
- max-width: 30px;
- }
- .dropdown {
- position: relative;
- display: inline-block;
- }
- .dropdown-content {
- display: none;
- position: absolute;
- background-color: #ffe8e8;
- min-width: 160px;
- padding: 20px 50px;
- }
- .dropdown:hover .dropdown-content {
- display: block;
- }
- #bought button{
- background-color: black; /* Green */
- border: none;
- color: white;
- padding: 15px 32px;
- text-align: center;
- text-decoration: none;
- display: inline-block;
- font-size: 20px;
- align-content: right;
- }
- #cart img{
- display: inline;
- }
- #cart p{
- display: inline;
- }
- #cart h4{
- display: inline;
- }
- </style>
- </head>
- <body onload="products();">
- <button onclick="cart.del_cart()">Delete Cart</button>
- <hr />
- <div id="bought">
- <div class="dropdown">
- <button onmouseover="cart.update();">Show cart</button>
- <div class="dropdown-content" id="cart">
- </div>
- </div>
- </div>
- <hr />
- <ul id="product" class="products">
- </ul>
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement