I have something like this:
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.
Hahahaha, posted this in the wrong forum originally. :D We're all human afterall. *cracks supermod knuckles*
drevicko
05-19-2006, 02:13 AM
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??
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.
Kravvitz
05-19-2006, 04:28 AM
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.
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.
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
Kravvitz
05-19-2006, 07:39 AM
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.
brothercake
05-19-2006, 12:01 PM
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.
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.