Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 12 of 12
  1. #1
    New Coder
    Join Date
    Jan 2013
    Posts
    16
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Shopping cart in javascript

    Hi there i just wanted to ask how would i go about creating a shopping cart in javascript. i have basically got an html website created styled with css, i have created products in the website but how would i create a javascript shopping cart for users to store thier items they want to buy? thanks

  • #2
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,618
    Thanks
    0
    Thanked 645 Times in 635 Posts
    JavaScript is not the best language to create a shopping cart - it is somethign better handled by the server.

    If you are going to use JavaScript to do it then the simplest way is to use frames - as I do with the script at http://www.felgall.com/jstip39.htm - note that JavaScript cannot handle the actual payment processing - you would need to submit all the shopping cart data to the server to get it processed there.
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • #3
    Regular Coder
    Join Date
    Jan 2013
    Location
    Germany
    Posts
    578
    Thanks
    4
    Thanked 77 Times in 77 Posts
    An alternative to frames would be using modern browser features like LocalStorage, SessionStorage, SQLite or IndexedDB.
    Except for SessionStorage, the probably most important benefit over frames would be that the shopping cart would be able to survive a page refresh as well as the customer coming back at a later point. However, he'd have to use the same browser again. If you want a shopping cart per user, you won't find a way around server-side storage such as MySQL databases.

  • #4
    The fat guy next door VIPStephan's Avatar
    Join Date
    Jan 2006
    Location
    Halle (Saale), Germany
    Posts
    8,628
    Thanks
    6
    Thanked 1,002 Times in 975 Posts
    And how do you intend to cater for users that either have no choice or deliberately disabled (some) JavaScript?
    I’m tired of websites not working just because someone doesn’t care to adhere to the most basic principles of best practice.

  • #5
    Regular Coder
    Join Date
    Jan 2013
    Location
    Germany
    Posts
    578
    Thanks
    4
    Thanked 77 Times in 77 Posts
    You could use the noscript tag for those people

    To be more serious: Of course you could also use a server-side database and let the user actually reload the page everytime they add something to the cart (to allow your server-side code to create the database entry). On top of that, for those that havr Javascript enabled, intercept the click with Javascript and use an Ajax call instead.

    The advantage, besides support for disabled Javascript, is storing the data server-side, which will make it accessible to users no matter from where they log in. If you don't use a login system, you can use, for example, a PHP session for non-JS users and a small localstorage entry for JS users. The disadvantage is having to store data server-side that you're typically not interested in storing as they are temporary.
    Last edited by Airblader; 04-10-2013 at 07:58 PM.

  • #6
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,618
    Thanks
    0
    Thanked 645 Times in 635 Posts
    Quote Originally Posted by Airblader View Post
    You could use the noscript tag for those people
    The noscript tag ought to have died with Netscape 4. There is never any need to use it with more modern browsers. You get far more flexibility by simply hiding those parts of the page with JavaScript.
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • #7
    Regular Coder
    Join Date
    Jan 2013
    Location
    Germany
    Posts
    578
    Thanks
    4
    Thanked 77 Times in 77 Posts
    Please note the "" behind that sentence. The obvious meaning being it was a joke, the hidden meaning being that in my opinion not having Javascript enabled is as obsolete as the noscript tag.

  • #8
    The fat guy next door VIPStephan's Avatar
    Join Date
    Jan 2006
    Location
    Halle (Saale), Germany
    Posts
    8,628
    Thanks
    6
    Thanked 1,002 Times in 975 Posts
    I like you, Airblader, for you are thinking in the right direction in post 5. I don’t agree with your opinion in post 7, though. For the stupid masses this might be true but I think an increasing amount of people is just as annoyed by the also increasing amount of crap that is loaded upon them and their system resources/bandwidth, especially when they are just browsing for something and don’t need advanced functionality that takes forever to load and makes sites slow and bloated, and/or want to be tracked and spied upon.

    There is a reason why browser extensions like AdBlock, Ghostery, NoScript, etc. exist, and it’s not just web developers or other geeks that are using those. And I already implied that people don’t necessarily have JS disabled completely; some might just block parts like third party scripts for exactly the above mentioned reasons. And more or less evil third party scripts are used and relied upon extensively these days which is part of the reason why many websites suck.

    And we’re not talking about a “rich internet application” like an online image editor here, that would indeed not work without JavaScript (just by the intrinsic nature of the program), we’re talking about a simple shopping cart. And ironically, those are the ones that are among the worst and least functional/user friendly websites in the world.

    Just because you can doesn’t mean you have to.

  • #9
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,618
    Thanks
    0
    Thanked 645 Times in 635 Posts
    Quote Originally Posted by VIPStephan View Post
    I don’t agree with your opinion in post 7,
    I also don't agree with the opinion in post 7.

    No matter how true you might consider that opinion to be true for general web pages - it is definitely false for shopping carts.

    With a shopping cart you have someone wanting to buy something from you. You are about to make some money provided that you provide the mans for them to actually complete the purchase.

    No matter how low a percentage of people don't have JavaScript enabled there is still the possibility that your buyer has it turned off or is using a browser that doesn't support it. If you rely on JavaScript to handle the shopping cart function then you are denying them the ability to buy from you and you earn less money as a result.

    That's why any shopping cart should be using a server side script for the core functionality. While it is possible to create a shopping cart script using JavaScript you will be missing out on sales if you do so.

    Anyway the actual payment processing is going to require server side code to actually process the payment so no matter how much of the processing you do in JavaScript you can't do all of it there.
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • #10
    Regular Coder
    Join Date
    Jan 2013
    Location
    Germany
    Posts
    578
    Thanks
    4
    Thanked 77 Times in 77 Posts
    Okay, I'll agree that for something like a shopping cart you shouldn't rely on Javascript. Still, in general my opinion is that it's the user's fault for not enabling a feature that is considered standard. But let's not start this discussion again. I provided a way to deal with non-js users nevertheless.

  • #11
    Banned
    Join Date
    Mar 2013
    Posts
    139
    Thanks
    0
    Thanked 9 Times in 9 Posts
    Quote Originally Posted by Airblader View Post
    Still, in general my opinion is that it's the user's fault for not enabling a feature that is considered standard.
    It's not a fault. It's their right to turn off js if they choose for whatever reason. And they don't have to give anyone a reason.

    So if a webdeveloper stupidly relies solely on a js shopping cart for their online business then imo they have only their stupidity to blame for any lost sales which will definitely occur.

    For a php programmer who knows what they are doing, a php shopping cart is fairly straight forward to set up. The online payment functionality requires server side processing whether your shopping cart is js or server side.

  • #12
    Regular Coder
    Join Date
    Jan 2013
    Location
    Germany
    Posts
    578
    Thanks
    4
    Thanked 77 Times in 77 Posts
    Once again:

    Quote Originally Posted by Airblader View Post
    let's not start this discussion again


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •