HTML tag <a> chce přidat jak href, tak onclick fungující

Již máte, co potřebujete, s menší změnou syntaxe:

<a href="www.mysite.com" onclick="return theFunction();">Item</a>

<script type="text/javascript">
    function theFunction () {
        // return true or false, depending on whether you want to allow the `href` property to follow through or not
    }
</script>

Výchozí chování <a> tagu onclick a href vlastnosti je spustit onclick a poté postupujte podle href tak dlouho jako onclick nevrací false , zrušení události (nebo události nebylo zabráněno)


Použijte jQuery. Musíte zachytit click a poté přejděte na web.

$("#myHref").on('click', function() {
  alert("inside onclick");
  window.location = "http://www.google.com";
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<a href="#" id="myHref">Click me</a>


Chcete-li toho dosáhnout, použijte následující html:

<a href="www.mysite.com" onclick="make(event)">Item</a>

<script>
    function make(e) {
        // ...  your function code
        // e.preventDefault();   // use this to NOT go to href site
    }
</script>

Zde je pracovní příklad.