modiX modiX - 2 months ago 7x
HTML Question

window.setTimeout in javascript link on Firefox not working properly

Due to generic reasons I have to put some javascript into the HREF attributes of some navigation bar links to perform some more actions. Partially actions require delayed execution, thus I'm using


Here is a sample:

<a href="javascript:alert('do anything');window.setTimeout(function(){alert('do anything else');},1111);">Login</a>

In Google Chrome this works like expected, but on Firefox it redirects to a new empty page that contains only a random number when
is called.


Why this is happening and how I can avoid that and still using HREF attributes for such actions? Specially the returned number is magic to me.


Wrapping it in an anonymous function seems to fix it:

<a href="javascript:(function(){alert('do anything');window.setTimeout(function(){alert('do anything else');},1111);})()">Login</a>

The number that is returned and displayed on the page in FF, is the return value of window.setTimeout, it's a counter incremented every time it is called, to allow you keep track of timers.