5

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.

3 Answers 3

9

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>
Sign up to request clarification or add additional context in comments.

1 Comment

I'm pretty sure you want to return popitup2() to maintain OP's current function.
2

Specify both of them in your link:

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

2 Comments

This syntax seems wrong. You cannot have multiple return statements in the onclick handler or at least it is not good.
without the return false in the function recordOutboundLink will it work in the same way?
1

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>

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.