javascripthtmlonclickhyperlinkajax-request

a href combi with javascript onClick


How do you make a javascript "onClick" and an html "a href" merge into one link? I want that when I click on the link the first step is the "onClick" and the second when the "onClick" is successed you redirected with the "a href" to another page.

  1. First step: onClick="ajaxrequest('/includes/docx/boekenlijst.php', 'docgegevens')

and

  1. Second step: a href="/Raymond_converters/docx/example.php">doc

I hope you can help me but my English is not so good. I'm sorry for that.


Solution

  • Using jQuery, you could try this:

    <script type="text/javascript">
        $(document).ready(function() {
            var call_ajax = true;
            $('#alink').click(function(e) {
                if (call_ajax) {
                    e.preventDefault();
                    // Do AJAX
                    call_ajax = false;
                }
            }
    </script>
    
    <a href="http://www.google.com" id="alink">google</a>