469,362 Members | 2,450 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,362 developers. It's quick & easy.

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 10604


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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Maria Bitsku | last post: by
4 posts views Thread by Doug Lerner | last post: by
9 posts views Thread by chrisspencer02 | last post: by
1 post views Thread by petermichaux | last post: by
2 posts views Thread by donpro | last post: by
1 post views Thread by CARIGAR | last post: by
1 post views Thread by Marylou17 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.