using preventDefault()

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Michael Powe

    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_re f").

    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.
  • Martin Honnen

    #2
    Re: using preventDefault( )



    Michael Powe wrote:

    [color=blue]
    > 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_re f").[/color]

    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="action A(); 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

    Comment

    • Michael Powe

      #3
      Re: using preventDefault( )

      >>>>> "Martin" == Martin Honnen <mahotrash@yaho o.de> writes:

      Martin> Michael Powe wrote:

      [color=blue][color=green]
      >> 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_re f").[/color][/color]

      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="action A(); 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.

      Comment

      • Martin Honnen

        #4
        Re: using preventDefault( )



        Michael Powe wrote:

        [color=blue]
        > "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.[/color]

        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

        Comment

        Working...