简体   繁体   中英

Javascript link with onClick event

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.

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.

That should be enough.

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

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); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM