views:

325

answers:

1

My jQuery script has a glitch animating a div position. The bug seems to occur when I click the link right as the page is loading.

+8  A: 

The bug seems to occur when I click the link right as the page is loading

This is because you are using the ready function and that executes as soon as the page has finished loading.

Thus, if you click on something whilst the page is still loading keep in mind that the JavaScript in that ready function has not been executed yet.


The reason why you can't always reproduce it most probably because the page will be cached the second time round and you wouldn't have time to click anything before the page has finished loading (since it's cached) and thus the js code would have been executed already when you click.

Andreas Grech
well, you can "apply the link" (enclose it in an a tag with a href) in the ready function itself. So, whilst the page is loading, there is no link and then you add the link in the function
Andreas Grech
or else, put code outside of the ready function (that will execute when the browser parses that part of the page, ie not when it has finished loading) that temporarily "removes" the link and then reapply it in the ready function
Andreas Grech