2010-07-09 6 views
5

Actuellement j'ai le code suivant sur un de mes web page-Appel deux fonctions javascripts onclick

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

maintenant je veux exécuter un script supplémentaire qui est utilisé de la manière suivante -

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

Maintenant quelqu'un peut-il me dire comment puis-je appeler ces deux javacsripts lorsque le lien est cliqué. Merci d'avance.

Répondre

9

Vous pouvez appeler les deux fonctions dans le gestionnaire d'événements onclick:

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

Pour éviter de mélanger le balisage avec javascript je vous recommande la fixation de la onclick événement pour ce lien particulier comme ceci:

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

Et en la section head:

<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> 
+0

Je suis sûr que vous voulez retourner 'popitup2()' pour maintenir la fonction actuelle de OP. –

2

Spécifiez deux d'entre eux dans votre lien:

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

Cette syntaxe semble erronée. Vous ne pouvez pas avoir plusieurs instructions 'return' dans le gestionnaire' onclick' ou du moins ce n'est pas bon. –

+0

@Darin Dimitrov: Bon point, mis à jour. Merci – Sarfraz

+0

sans le retour faux dans la fonction recordOutboundLink cela fonctionnera-t-il de la même manière? – ayush

1

Vous pouvez le faire avec une fermeture:

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

ou tout simplement une meilleure commande:

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