pjknight
pjknight

Reputation: 130

Tracking external link clicks

I want to post some data to a file via ajax when somebody clicks a link. I have the following code which works in all the main browsers except IE :(

    <script type="text/javascript">
        $(document).ready(function() {

           $('a').click(function() {

                $.ajax({

                    url: '<?php echo SITE_URL ?>stats/add_click/',
                    type: 'POST',
                    datatype: 'html',
                    data: {

                        id: $(this).attr('data-id'),
                        ip: '<?php echo $_SERVER['REMOTE_ADDR'] ?>'

                    }

                });

                return true;

           });

        });
    </script>

How do I get this to work in IE or debug the code for IE?

Regards

Upvotes: 0

Views: 126

Answers (2)

charlietfl
charlietfl

Reputation: 171679

Try using the success callback of ajax to make the redirect. Sounds like IE is aborting the request

$('a').click(function() {
            $.ajax({
                url: '<?php echo SITE_URL ?>stats/add_click/',
                type: 'POST',
                dataType: 'html',
                data: {
                    id: $(this).attr('data-id'),
                    ip: '<?php echo $_SERVER['REMOTE_ADDR'] ?>'
                },                  
                success:function(){
                    window.location = this.href;
                }

            });

            return false;

       });

Upvotes: 2

Rodolfo
Rodolfo

Reputation: 4183

try this...

$('a').click(function(e) {
    e.preventDefault();
    ...

Upvotes: 0

Related Questions