R
Robert
Hi,
I have noticed some synchronization issues when using javascript.
I'll give you an example. It is easy to reproduce the problem if you can
cause some delay in the webserver before sending the page.
//filename = page.html
<script type="text/javascript">
function test1()
{
}
</script>
<a href="#" onclick="test1();
window.location.reload();
return false;">Refresh</a>
When I click the link quite fast then I get some javascript errors about
how test1 does not exist. As a side-effect I will be directed to page.html#
This problem occurs in Firefox 1.5 and not in Internet Explorer 6. I
only tried these browsers for now.
What I am thinking what happens is that the onclick events are handled
sequentially, however the reload is started in a different thread, and
does not stop the UI from sending new events. So the onclick event is
executed while at that time the current page is unloaded. The test1
function does not exist anymore and so causes a javascript error in the
onclick event. This also causes the onclick to not return default and
therefore the href will be used to go to the next page. So now two
threads are racing against eachother. One to reload and one to use the
href. So I will get either page.html or page.html# in the next url. In
this case the difference might not be so interesting, however if I used
window.location.href="next.html" the problem can be quite serious.
So how do I effectively protect me from these problems? And are there
more synchronization issues that I should be aware of in these or other
browers?
For protection I was thinking of something like this:
<a href="#" onclick="try {
test1();
window.location.reload();
}
catch(e)
return false;">Refresh</a>
But I do not like this because I would not get any javascript errors if
I made some mistake when implementing test1 and would make debugging
more difficult.
Any comments are appreciated.
Robert
I have noticed some synchronization issues when using javascript.
I'll give you an example. It is easy to reproduce the problem if you can
cause some delay in the webserver before sending the page.
//filename = page.html
<script type="text/javascript">
function test1()
{
}
</script>
<a href="#" onclick="test1();
window.location.reload();
return false;">Refresh</a>
When I click the link quite fast then I get some javascript errors about
how test1 does not exist. As a side-effect I will be directed to page.html#
This problem occurs in Firefox 1.5 and not in Internet Explorer 6. I
only tried these browsers for now.
What I am thinking what happens is that the onclick events are handled
sequentially, however the reload is started in a different thread, and
does not stop the UI from sending new events. So the onclick event is
executed while at that time the current page is unloaded. The test1
function does not exist anymore and so causes a javascript error in the
onclick event. This also causes the onclick to not return default and
therefore the href will be used to go to the next page. So now two
threads are racing against eachother. One to reload and one to use the
href. So I will get either page.html or page.html# in the next url. In
this case the difference might not be so interesting, however if I used
window.location.href="next.html" the problem can be quite serious.
So how do I effectively protect me from these problems? And are there
more synchronization issues that I should be aware of in these or other
browers?
For protection I was thinking of something like this:
<a href="#" onclick="try {
test1();
window.location.reload();
}
catch(e)
return false;">Refresh</a>
But I do not like this because I would not get any javascript errors if
I made some mistake when implementing test1 and would make debugging
more difficult.
Any comments are appreciated.
Robert