0

I am using a third-party shopping cart from http://simplecartjs.com/ . For a normal checkout I can use:

<a href="javascript:;" class="simpleCart_checkout" >Checkout</a>

And it works. But I need to add some server-side functionality and don't know how to go about this. The code inside the javascript file where the simpleCart_Checkout class is stored is as follows:

me.addEventToArray( getElementsByClassName('simpleCart_checkout') , simpleCart.checkout , "click");

EDIT: and this:

me.checkout = function() {
        if( me.quantity === 0 ){
            error("Cart is empty");
            return;
        }
        switch( me.checkoutTo ){
            case PayPal:
                me.paypalCheckout();
                break;
            case GoogleCheckout:
                me.googleCheckout();
                break;
            case Email:
                me.emailCheckout();
                break;
            default:
                me.customCheckout();
                break;
        }
    };

So I tried doing it using a button calling the method directly:

<asp:Button ID="CheckoutButton" runat="server" Text="Checkout" 
            onclick="CheckoutButton_Click" OnClientClick="Checkout()" />

            <script type="text/javascript">
                function Checkout() {

                    javascript: simpleCart.checkout;
                }    
                </script>

Which calls the server-side but doesn't call the javascript link. I am new to asp.net and javascript so don't really know any other ways of how I can do this, please help.

3 Answers 3

1

try this:

<asp:Button ID="CheckoutButton" runat="server" Text="Checkout" 
            onclick="CheckoutButton_Click" OnClientClick="javascript:Checkout();" />

        <script type="text/javascript">
            function Checkout() {

               simpleCart.checkout();
               return true;

            }    
            </script>

Edit: you want your scripts being called after the server event. then you'll need to call your Checkout function at the end of "CheckoutButton_Click".

Page.ClientScript.RegisterStartupScript(Page.GetType(), "calling checkout", "<script>CheckOut();</script>", true);

simpleCart.checkout() doesn't got a chance to do redirect, as OnClientClick returns true and post back happens.

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

4 Comments

Same thing happens, server-side is called and posts back but doesn't execute the javascript.
are you sure you have referenced simpleCart? put an alert('xxx') in your Checkout function to verify your scripts is called.
Ok the alert worked so the javascript is being called, it just isn't finding the right simpleCart method. I edited my answer to show all instances of checkout in simpleCart.js.
Sorry it took so long to answer. There must be a problem in the simpleCart javascript file which I'm not seeing because I'm new to javascript.
0

That should be enough.

function Checkout() {
    simpleCart.checkout();
    return true;
}

Comments

0

You can call your javascript from Server side.

Page.RegisterStartupScript will assists you to fire javascript from code behind.

Page.ClientScript.RegisterStartupScript(Page.GetType(), Guid.NewGuid().ToString(), "alert('hello')", true); 

Comments

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.