0
<a href="http://stackoverflow.com/" class="link" id="4" >stackoverflow</a>

<script>
$('.link').click(function(){
     var idx = $(this).attr('id');
     visit(idx);
})
</script>

I want to make a function run before browser opened.

The function visit() is defined and doesn't have error.

For some reason, visit(idx) is ignored.

Can anyone give me any hints?

jerome
  • 91
  • 10

1 Answers1

0

You have to prevent default behaviour of clicked link:

$('.link').click(function(e){
     e.preventDefault();
     var idx = this.id;
     visit(idx);
})

Now depending what visit() function is doing, you can after it completes (i guess it is async method), redirect browser to specific link as it would be done clicking on former link.

For example, inside click handler and supposing visit() returns a promise (ajax request e.g):

visit(idx).always(function(){this.click();}.bind(this)); or using window.location = this.href;

But for more specific help, you need to post visit() method code in question itself.

A. Wolff
  • 74,033
  • 9
  • 94
  • 155