Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    jkd
    jkd is offline
    Senior Coder jkd's Avatar
    Join Date
    May 2002
    Location
    metro DC
    Posts
    3,163
    Thanks
    1
    Thanked 18 Times in 18 Posts

    How to cancel a DOM event in Safari?

    I have something like this:
    Code:
    document.addEventListener("click", function(event) {
        if ([conditions]) {
            // do stuff
            event.preventDefault();
        }
    }, false);
    Clicking on a link, for example, will still follow the url to the next page, instead of being cancelled. I've tried event.returnValue = false and return false (and all 3 combined) with no success.

    Does Safari support preventDefault()? (I know the method is there, but it doesn't seem to do anything.) If so, I might have other ideas to the problem...

    If not, does it support something equivalent?

    Thanks.


    Edit: Hahahaha, posted this in the wrong forum originally. We're all human afterall. *cracks supermod knuckles*
    Last edited by jkd; 01-03-2004 at 11:28 PM.

  • #2
    New to the CF scene
    Join Date
    May 2006
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts
    A web search on this topic only found other people with the same trouble.

    I've been using preventDefault to add special behaviour to links (ie:nice popups) when scripting is on (and have W3C accessible links if its not). These links fire twice in Safari (since the default is not being prevented).

    The easier solution I see is to detect Safari, and simply not do the special behaviour.

    The harder solution is to detect Safari in a launch page, and add a GET variable to the URL (like safariScripting=1), then not include the URL's in the links serverside. The default then has no action and the links fire just once.

    Peter Blumm has a more difficult problem here:

    http://www.peterblum.com/SafariBugs/CancelKeyPress.html

    Maybe he can detect Safari and explicitly controll the content of the input box??

  • #3
    jkd
    jkd is offline
    Senior Coder jkd's Avatar
    Join Date
    May 2002
    Location
    metro DC
    Posts
    3,163
    Thanks
    1
    Thanked 18 Times in 18 Posts
    If you're doing links, my work aroundaround was to do something like:

    var oldHref = link.href;
    link.href="javascript://";
    setTimeout(function() { link.href = oldHref }, 1);

    So after the event handler is called, it still follows the link, which does nothing, then sets the href back afterwards.

  • #4
    Senior Coder
    Join Date
    Feb 2006
    Location
    USA
    Posts
    1,013
    Thanks
    0
    Thanked 0 Times in 0 Posts
    jkd is correct, though both link and oldHref would need to be global variables for that to work. I've used a similar hack myself to account for that bug in Safari.
    Code:
      if(e.preventDefault) {
        e.preventDefault();
        if(navigator.userAgent.toLowerCase().indexOf('safari')!=-1) {
          window.templink = el;
          window.temphref = el.href;
          el.href='javascript:void(0);';
          setTimeout('templink.href=temphref;temphref=null;templink=null;',100)'
        }
      }
    If anyone knows of a way to detect Safari 1.x with object detection, I would appreciate it if you would tell me.
    Last edited by Kravvitz; 05-19-2006 at 04:30 AM.
    Learn CSS. | SSI | PHP includes | X/HTML Validator | CSS validator | Dynamic Site Solutions
    Java != JavaScript && JScript != JavaScript
    Design/program for Firefox (and/or Opera), apply fixes for IE, not the other way around.

  • #5
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    Quote Originally Posted by Kravvitz
    If anyone knows of a way to detect Safari 1.x with object detection, I would appreciate it if you would tell me.
    try
    http://developer.apple.com/internet/safari/faq.html
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  • #6
    Senior Coder
    Join Date
    Feb 2006
    Location
    USA
    Posts
    1,013
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I have seen that before. I looked through it again, but don't see a way to detect Safari 1.x with object detection. I suppose you could detect it via the process of elimination, but I'm not sure that would be better than just checking the user agent string.
    Learn CSS. | SSI | PHP includes | X/HTML Validator | CSS validator | Dynamic Site Solutions
    Java != JavaScript && JScript != JavaScript
    Design/program for Firefox (and/or Opera), apply fixes for IE, not the other way around.

  • #7
    Senior Coder
    Join Date
    Jun 2002
    Location
    near Oswestry
    Posts
    4,508
    Thanks
    0
    Thanked 0 Times in 0 Posts
    You can detect Safari with navigator.vendor, which always has the value "Apple Computer, Inc."

    You can differentiate 1.2 or later from earlier versions by the support for the XMLHttpRequest object

    This event cancelling problem in Safari is a long standing bug .. but it only happens with event listeners; if you use a DOM0 onclick handler it will work.
    "Why bother with accessibility? ... Because deep down you know that the web is attractive to people who aren't exactly like you." - Joe Clark

  • #8
    jkd
    jkd is offline
    Senior Coder jkd's Avatar
    Join Date
    May 2002
    Location
    metro DC
    Posts
    3,163
    Thanks
    1
    Thanked 18 Times in 18 Posts
    Quote Originally Posted by Kravvitz
    jkd is correct, though both link and oldHref would need to be global variables for that to work.
    No, it's called scope. The anonymous function you pass to the setTimeout() examines the current scope as its identifier lookup ascends the scope chain.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •