Troubles Rewriting OnClick Event

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Jamie Jackson

    #1

    Troubles Rewriting OnClick Event

    I'm rewriting all links' onclick events, but I'm having a problem. The
    onclick event that I'm inserting works correctly in Opera, but not in
    FF or IE.

    I'm retroactively adding the statement "return promptBeforeOpe ning();"
    [promptBeforeOpe ning() is my own custom method.]

    FF and IE rewrite it as I would expect them to, but it turns out that
    that doesn't work (no disclaimer pops up, and the link is not
    disabled):
    (Before Rewriting)
    onClick:undefin ed
    (After Rewriting)
    onClick:return promptBeforeOpe ning();

    Opera seems to do me a favor, and makes it work for me:
    (Before Rewriting)
    onClick:undefin ed
    (After Rewriting)
    onClick:functio n anonymous(event ) {return promptBeforeOpe ning();}
    ^^^^^^^^^^^^^^^ ^^^^^^^^^^^^ ^

    See how Opera adds that weird method definition? Whatever that is, it
    works.

    So, the question is, what do I need to do to get it to work in the
    other browsers?

    Thanks,
    Jamie

    <a href="http://www.adcouncil.o rg/campaigns/adoption/" target="new">my
    link</a>

    <script type="text/javascript">
    function promptBeforeOpe ning () {
    if (confirm("LEAVI NG SITE X\n\nYou are about to leave site x to go
    to another location that is not maintained by x. X takes no
    responsibility for and exercises no control over the views that may be
    represented, or the accuracy, privacy policies, copyright or trademark
    compliance, or the legality of any material contained on external
    sites. \n\nTo proceed to the Web site, please select the OK
    button.")) {
    // confirmed: thank you, drive through
    return true;
    } else {
    // user has cancelled, don't go to link
    return false;
    }
    }


    for (var i=0; i < document.links. length; i++) {
    // save a copy of any onClick that the link may have originally had
    // debug output
    document.write( "<h4 style='color: blue'>(Before) onClick:" +
    document.links[i].onclick + " Href:" + document.links[i].href +
    "</h4>");
    origOnClick = document.links[i].onclick;
    // rewrite the link's onClick to prompt before going
    document.links[i].onclick = "return promptBeforeOpe ning();";
    if (origOnClick != null) {
    // old onClick exists, so append it to the new one
    document.links[i].onclick += origOnClick;
    }
    // debug output
    document.write( "<h4 style='color: blue'>(After) onClick:" +
    document.links[i].onclick + " Href:" + document.links[i].href +
    "</h4>");
    }

    </script>
  • Martin Honnen

    #2
    Re: Troubles Rewriting OnClick Event



    Jamie Jackson wrote:

    [color=blue]
    > for (var i=0; i < document.links. length; i++) {
    > // save a copy of any onClick that the link may have originally had
    > // debug output
    > document.write( "<h4 style='color: blue'>(Before) onClick:" +
    > document.links[i].onclick + " Href:" + document.links[i].href +
    > "</h4>");
    > origOnClick = document.links[i].onclick;
    > // rewrite the link's onClick to prompt before going
    > document.links[i].onclick = "return promptBeforeOpe ning();";[/color]

    The onclick property is usually a function object and not a string (test
    typeof origOnClick to check that), thus if you want to assign a
    different event handler you need to assign a function. There are
    different ways to construct one, you can use a function expresssion:
    document.links[i].onclick = function (evt) {
    return promptBeforeOpe ning();
    };
    or you can use the Function constructor to create a function
    document.links[i].onclick = new Function("evt",
    "return promptBeforeOpe ning();"
    );
    or you can declare a gobal function
    function newClickHandler (evt) {
    return promptBeforeOpe ning();
    }
    and assign it
    document.links[i].onclick = newClickHandler ;
    [color=blue]
    > if (origOnClick != null) {
    > // old onClick exists, so append it to the new one
    > document.links[i].onclick += origOnClick;[/color]

    Again string operation doesn't help, what could help is
    var link = document.links[i];
    link.originalOn click = link.onclick;
    link.onclick = function (evt) {
    if (this.originalO nclick) {
    this.originalOn click(evt);
    }
    return promptBeforeOpe ning();
    };
    but depending on what browsers you target you could avoid scripting
    onclick as a property and instead use attachEventList ener (W3C DOM in
    Mozilla, Netscape 6/7, Opera 7) or attachEvent (MSIE 5.5/6 DOM) to add
    an additional event handler as needed.

    --

    Martin Honnen
    http://JavaScript.FAQTs.com/

    Comment

    • Grant Wagner

      #3
      Re: Troubles Rewriting OnClick Event

      Jamie Jackson wrote:
      [color=blue]
      > So, the question is, what do I need to do to get it to work in the
      > other browsers?[/color]

      document.links[i].onclick is a reference to the function that handles the
      -onclick- event, not a string containing the JavaScript within that
      function. So you assign the reference of the function you want to assign
      to the -onclick- event:

      // note: no brackets on the method
      document.links[i].onclick = promptBeforeOpe ning;
      or:
      // if you wanted to pass parameters to the handler
      document.links[i].onclick = function() { return
      promptBeforeOpe ning(something) ; }

      The first one should work for your code.

      --
      Grant Wagner <gwagner@agrico reunited.com>
      comp.lang.javas cript FAQ - http://jibbering.com/faq

      Comment

      • Jamie Jackson

        #4
        Re: Troubles Rewriting OnClick Event

        Thank you very much, Martin and Grant. That's a BIG help.

        Jamie

        Comment

        Working...