ayush
ayush

Reputation: 14568

Calling two javascripts functions onClick

Presently i have the following code on one of my web page-

<a href="http://ex.com" onclick="return popitup2()">Grab Coupon</a>

now I want to run one more script which is used in the following way -

onClick="recordOutboundLink(this, 'Outbound Links', 'ex.com');return false;"

Now can someone tell me how do i call both of these javacsripts when the link is clicked. Thanks in advance.

Upvotes: 5

Views: 34306

Answers (3)

Darin Dimitrov
Darin Dimitrov

Reputation: 1038730

You can call the two functions in the onclick event handler:

<a href="http://ex.com" onclick="popitup2(); recordOutboundLink(this, 'Outbound Links', 'ex.com'); return false;">Grab Coupon</a>

To avoid mixing markup with javascript I would recommend you attaching the onclick event for this particular link like this:

<a href="http://ex.com" id="mylink">Grab Coupon</a>

And in the head section:

<script type="text/javascript">
window.onload = function() {
    var mylink = document.getElementById('mylink');
    if (mylink != null) {
        mylink.onclick = function() {
            var res = popitup2(); 
            recordOutboundLink(this, 'Outbound Links', 'ex.com');
            return res;
        };
    }
};
</script>

Upvotes: 9

Sarfraz
Sarfraz

Reputation: 382676

Specify both of them in your link:

<a href="http://ex.com" onclick="recordOutboundLink(this, 'Outbound Links', 'ex.com'); return popitup2();">Grab Coupon</a>

Upvotes: 2

Mark Elliot
Mark Elliot

Reputation: 77034

You can do it with a closure:

<a href="http://ex.com" onclick="return function(){ recordOutboundLink(this, 'Outbound Links', 'ex.com'); return popitup2(); }()">Grab Coupon</a>

or just some better ordering:

<a href="http://ex.com" onclick="recordOutboundLink(this, 'Outbound Links', 'ex.com');return popitup2();">Grab Coupon</a>

Upvotes: 1

Related Questions