SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Enthusiast
    Join Date
    Sep 2005
    Posts
    30
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question Preventing default event in Opera and Netscape

    Hi,

    I'm workin on a web application that can not only be used with the mouse but also via keyboard shortcuts, arrow keys etc., just as you would use a desktop app.
    Yahoo mail is a good example for this kind of application though it's a bit overdone

    In order to do this I have to prevent some default browser events like scrolling on arrow down, changing focus when pressing tab, selecting text on ctrl + a etc.

    Code:
    function stopDefaultAction(event)
    {
    	// IE
    	event.returnValue = false;
    	
    	// mozilla
    	if(typeof event.preventDefault != 'undefined')
    	{
    		event.preventDefault();
    	}
    }
    This is the code I'm trying to cancel the default events with and Firefox as usual works as it should. Netscape and Opera though seem to ignore this and still perform their own actions.

    How can this be fixed or do these browsers simply not support canceling their default actions?

    Cheers, Florian

  2. #2
    SitePoint Author silver trophybronze trophy

    Join Date
    Nov 2004
    Location
    Ankh-Morpork
    Posts
    12,158
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    event.preventDefault() works perfectly in Opera. Are you sure you are sending the correct event object to this function? Opera9 supports window.event, just like IE, so checking for the presence of that object can cause strange things to happen.

    I spent half a day looking for a problem with the Prototype library, which turned out to be caused by their choice of variable name: event, just like your code. Opera sees this as the global event object, i.e., window.event.

    Try changing the name of your function argument:
    Code:
    function stopDefaultAction(ev)
    {
    	// IE
    	ev.returnValue = false;
    	
    	// mozilla
    	if(typeof ev.preventDefault != 'undefined')
    	{
    		ev.preventDefault();
    	}
    }
    Birnam wood is come to Dunsinane


Bookmarks

Posting Permissions

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