473,398 Members | 2,380 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

Synchronization issues

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
Apr 14 '06 #1
7 5389
Robert wrote:
This also causes the onclick to not return default and ...


Correction: This also causes the onclick to not return false and so the
default action will happen for the A element, which is to open the url
in the href.
Apr 14 '06 #2
VK

Robert wrote:
Correction: This also causes the onclick to not return false and so the
default action will happen for the A element, which is to open the url
in the href.


So don't misuse hypertext container then if you don't need any
navigation. Use <var> or (if you are semantically preoccupied :-)
<span>

var {
font-style: normal;
color: #0000FF;
text-decoration: underline;
cursor: pointer;
behavior: url(hilite.htc);
}

The behavior is needed only until IE 7 release which will support hover
for all elements. A two-line code for .htc can be found here:
<http://groups.google.com/group/comp.lang.javascript/tree/browse_frm/thread/85eff0714f7c2778/3238327268eed329?rnum=1&_done=%2Fgroup%2Fcomp.lang .javascript%2Fbrowse_frm%2Fthread%2F85eff0714f7c27 78%2F9e263b68e08d9c2b%3F#doc_d07a65ad5c9ab43b>

And then:

<var onclick="f1()">Click here</var>

It doesn't solve the problem of missing function, but no worries about
unwanted navigation.

Even more robust:
<var onclick="if ('undefined' != typeof f1) {f1();}">Click here</var>

Apr 14 '06 #3
VK wrote:
Robert wrote:
Correction: This also causes the onclick to not return false and so the
default action will happen for the A element, which is to open the url
in the href.

So don't misuse hypertext container then if you don't need any
navigation. Use <var> or (if you are semantically preoccupied :-)
<span>


Hmm I will think about that. Not exactly why you prefer VAR over SPAN,
but I like the idea of not using A.
Of course this was done because of the hover support as you have guessed.
Even more robust:
<var onclick="if ('undefined' != typeof f1) {f1();}">Click here</var>


Not really worth it in my opinion because there is nothing stopping f1
from being unloaded between the if condition and f1() statement.
Apr 14 '06 #4
VK

Robert wrote:
Not exactly why you prefer VAR over SPAN,


Only for the sake of convenience: this way I can get all psi-links in
one call:
document.getElementsByTagName('VAR');
rather than grab all <span>'s (which I use for other purposes too) and
then sort them out in a loop.

Apr 14 '06 #5
VK wrote:
Robert wrote:
Not exactly why you prefer VAR over SPAN,

Only for the sake of convenience: this way I can get all psi-links in
one call:
document.getElementsByTagName('VAR');
rather than grab all <span>'s (which I use for other purposes too) and
then sort them out in a loop.


Ahh I see...
Well I guess I am semantically preoccupied :)

Apr 14 '06 #6
VK

Robert wrote:
Well I guess I am semantically preoccupied :)


Oh, I'm so sorry... :-(

:-)

Apr 14 '06 #7
VK

Robert wrote:
Not exactly why you prefer VAR over SPAN,


Only for the sake of convenience: this way I can get all psi-links in
one call:
document.getElementsByTagName('VAR');
rather than grab all <span>'s (which I use for other purposes too) and
then sort them out in a loop.

Ahh I see...
Well I guess I am semantically preoccupied :)


Also: despite widely believed otherwise, a hypercontainer doesn't
require neither href nor name attributes. These attributes only needed
to make a hypercontainer act as a link or as an anchor or as both. It
is totally correct and standard-compliant just to mark a hypercontainer
but leave it without the regular functionality:
<a>Just a plain hypercontainer</a>

But the default styling (cursor, color) is attached not to the
hypercontainer itself, but only to a hypercontainer used as a link
(having href attribute set). So additional styling is still needed:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Strict//EN"
"http://www.w3.org/TR/html401/strict.dtd">
<html>
<head>
<title>Test</title>
<meta http-equiv="Content-Type"
content="text/html; charset=iso-8859-1">
<style type="text/css">
a {
color: #0000FF;
text-decoration: none;
cursor: pointer;
behavior: url(hilite.htc) /* IE4/5/6 hover fix */}
a:hover {
color: #FF0000;
text-decoration: underline}
</style>
</head>
<body>
<p><a onClick="alert(this.innerHTML)">Click me</a></p>
</body>
</html>

Where hilite.htc is (I edited it a bit to make it more flexible):

<public:component>
<public:attach event="onmouseover" onevent="Hilite()">
<public:attach event="onmouseout" onevent="Restore()">
<script type="text/Jscript">
// Your settings:
var hoverColor = '#FF0000';
var hoverDecor = 'underline';
// End of settings

var normalColor, normalDecor;

function Hilite() {
normalColor = this.currentStyle.color;
normalDecor = this.currentStyle.textDecoration;
this.runtimeStyle.color = hoverColor;
this.runtimeStyle.textDecoration = hoverDecor;
this.runtimeStyle.cursor = 'hand';
}

function Restore() {
this.runtimeStyle.color = normalColor;
this.runtimeStyle.textDecoration = normalDecor;
}
</script>
</public:component>

You are welcome to use this approach too. I'm still missing the
semantical difference of using <a href> or <a> just to execute
JavaScript on the same page (which many hold as semantically correct
approach) and using <var> or other non-<a> tag (which is considered as
semantically non-correct). But the W3C-style semantic thinking is a
dark matter I guess :-)

Apr 15 '06 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Marshal Antony | last post by:
Hi, I have a web service creates a # delimited text file on a server different than the web server per day for tracking databse transactions.All transactions will be writing to this same text...
4
by: omeropee | last post by:
Hi, Our company is an independent Voice applications solution provider with number clients using our suite. We have a CT application suite which is running with Application Server and SQL Server...
0
by: JDR | last post by:
I am trying to implement an abstract query factory in .NET. My solution uses a Factory that creates a concrete command class using reflection and caches an instance of it in a HashTable. Each...
7
by: Ivan | last post by:
Hi there My work on threads continues with more or less success. Here is what I'm trying to do: Class JobAgent is incharged for some tasks and when it's called it starts thread which performs...
5
by: Cyrus | last post by:
I have a question regarding synchronization across multiple threads for a Hashtable. Currently I have a Threadpool that is creating worker threads based on requests to read/write to a hashtable....
0
by: Rod | last post by:
I orginally posted this to microsoft.public.sqlserver.ce but had not received any responses. I have a CF.NET application (C#) with a SqlCE database. We had originally planned to use SQL...
12
by: emma_middlebrook | last post by:
Hi Say you had N threads doing some jobs (not from a shared queue or anything like that, they each know how to do their own set of jobs in a self-contained way). How can you coordinate them so...
5
by: Tony Gravagno | last post by:
I have a class that instantiates two Timer objects that fire at different intervals. My class can be instantiated within a Windows Form or from a Windows Service. Actions performed by one of the...
0
by: sundman.anders | last post by:
Hi all! I have a question about thread synchronization and c++ streams (iostreams, stringstreams, etc). When optimizing a program for a multicore processor I found that stringstream was causing...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.