473,511 Members | 17,486 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

using preventDefault()

Hello,

I am trying to work out the usage of preventDefault() and not having
any success.

The scenario is that I want to add on onclick handler to a link. When
the onclick handler fires, I want action A to take place, which will
be a request. I want to insure that action A completes before the
click to the link retrieves the page. To do that, I would like to
issue a preventDefault(), wait for action A to complete and then
direct the browser to the page specified in the link (e.g., set
location.href="clicked_link_ref").

I just cannot find any practical information that leads me to a way to
do this. I have googled and read the O'Reilly book on JS and the JS
cookbook, as well. I haven't been able to backtrack from their
examples to a solution to my own problem.

Any help would be much appreciated.

Thanks.

mp

--
'cat' is not recognized as an internal or external command,
operable program or batch file.
Aug 30 '05 #1
3 10780


Michael Powe wrote:

I am trying to work out the usage of preventDefault() and not having
any success.

The scenario is that I want to add on onclick handler to a link. When
the onclick handler fires, I want action A to take place, which will
be a request. I want to insure that action A completes before the
click to the link retrieves the page. To do that, I would like to
issue a preventDefault(), wait for action A to complete and then
direct the browser to the page specified in the link (e.g., set
location.href="clicked_link_ref").


I don't know what "action A" is but if you have a simple script function
name actionA then you can call it in the onclick handler e.g.
<a href="whatever.html"
onclick="actionA(); return true;">link</a>
and then when the link is clicked the browser calls the onclick handler
and processes that before loading the href URL.

Or what exactly is "action A" supposed to be that you think you need to
tell the browser to wait and that preventDefault would do that?

--

Martin Honnen
http://JavaScript.FAQTs.com/
Aug 30 '05 #2
>>>>> "Martin" == Martin Honnen <ma*******@yahoo.de> writes:

Martin> Michael Powe wrote:

I am trying to work out the usage of preventDefault() and not
having any success. The scenario is that I want to add on
onclick handler to a link. When the onclick handler fires, I
want action A to take place, which will be a request. I want
to insure that action A completes before the click to the link
retrieves the page. To do that, I would like to issue a
preventDefault(), wait for action A to complete and then direct
the browser to the page specified in the link (e.g., set
location.href="clicked_link_ref").


Martin> I don't know what "action A" is but if you have a simple
Martin> script function name actionA then you can call it in the
Martin> onclick handler e.g. <a href="whatever.html"
Martin> onclick="actionA(); return true;">link</a> and then when
Martin> the link is clicked the browser calls the onclick handler
Martin> and processes that before loading the href URL.

Martin> Or what exactly is "action A" supposed to be that you
Martin> think you need to tell the browser to wait and that
Martin> preventDefault would do that?

"Action A" sends a request for an image. This doesn't work in Firefox
and Mozilla because they don't wait for the request to complete before
proceeding with the opening of the link. The opening of the link
causes them to close the request, so it is never completed. The
mozilla dev team has told me that this is done according to the HTTP
specification and it is not a bug. IE seems to either queue the
request or wait for it; in any event, it will be completed in IE but
not in the others. It was a guy at Mozilla who suggested to me the
technique outlined above.

Additionally, it irritates me that I can't figure out how to make it
work. ;-) So I want to work it out. I'm open to suggestions of
resources to get a better understanding of the problem, too.

Thanks.

mp
--
'cat' is not recognized as an internal or external command,
operable program or batch file.
Aug 30 '05 #3


Michael Powe wrote:

"Action A" sends a request for an image. This doesn't work in Firefox
and Mozilla because they don't wait for the request to complete before
proceeding with the opening of the link. The opening of the link
causes them to close the request, so it is never completed. The
mozilla dev team has told me that this is done according to the HTTP
specification and it is not a bug. IE seems to either queue the
request or wait for it; in any event, it will be completed in IE but
not in the others. It was a guy at Mozilla who suggested to me the
technique outlined above.


You can use preventDefault once in the onclick handler but you can't
have the onclick handler block while the image is being loaded. So you
would need to use the onload handler of the image to later follow the
link e.g. alike
<a href="whatever.html"
onlick="var img = new Image();
var link = this;
img.onload = function (evt) {
window.location.href = link.href;
};
img.onerror = function (evt) {
// if you want to have the link href URL loaded
// even if the image is not loadable:
window.location.href = link.href;
}
img.src = 'whatever.gif';
return false;">link</a>

--

Martin Honnen
http://JavaScript.FAQTs.com/
Aug 30 '05 #4

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

Similar topics

3
11589
by: Maria Bitsku | last post by:
How do I deactivate a window using Javascript. For example if I have a window that opens up another window, how do I prevent the user from clicking (doing anything) in the original window until...
2
2099
by: Mike | last post by:
I have a textbox where I'm trying to restrict which keys a user can hit. My syntax is onKeyDown="myFunction(event)" To restrict the keys, Netscape requires the event.preventDefault method. ...
4
9912
by: Doug Lerner | last post by:
In the following script: function txKeyPressHandler(theEvent) { var key = theEvent.which || theEvent.keyCode; switch (key) { case Event.KEY_RETURN: txIMSendMsg(); var userAgent =...
13
2077
by: Andy Baxter | last post by:
Can anyone recommend a good online guide to using objects in javascript? The book I bought (DHTML Utopia) suggests using objects to keep the code clean and stop namespace clashes between different...
9
3482
by: chrisspencer02 | last post by:
I am looking for a method to extract the links embedded within the Javascript in a web page: an ActiveX component, or example code in C++/Pascal/etc. I am looking for a general solution, not one...
1
1901
by: petermichaux | last post by:
Hi, The Yahoo! UI event library goes to extremely great lengths to solve this problem. Their solution is very creative but uses browser sniffing. In Safari 1.3 (and earlier?) the following...
1
9100
by: Charlie | last post by:
I want to detect and act on right button clicks in Firefox. The problem was I kept getting the context menu pop up on right button clicks, even when I called event.preventDefault(). I had assumed...
3
8210
by: JDeats | last post by:
I have some .NET 1.1 code that utilizes this technique for encrypting and decrypting a file. http://support.microsoft.com/kb/307010 In .NET 2.0 this approach is not fully supported (a .NET 2.0...
2
2040
by: donpro | last post by:
Hi, I have a form with date fields, I have created event listeners to check for a validate when leaving the field (onblur). If invalid, I need it to set the field back to the default value,...
0
7245
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7356
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
7427
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...
1
7085
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
7512
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
5671
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
1577
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
785
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
449
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.