Microsoft KB Archive/231322

From BetaArchive Wiki
Knowledge Base


INFO: Override a Default Action in Internet Explorer 5 by Canceling the Event

Article ID: 231322

Article Last Modified on 5/11/2006



APPLIES TO

  • Microsoft Internet Explorer 4.0 128-Bit Edition
  • Microsoft Internet Explorer 4.01 Service Pack 2
  • Microsoft Internet Explorer 4.01 Service Pack 1
  • Microsoft Internet Explorer 4.01 Service Pack 2
  • Microsoft Internet Explorer 5.0
  • Microsoft Internet Explorer 5.5



This article was previously published under Q231322

SUMMARY

You can override an event's default action by setting the returnValue property on the window.event object to false. This will cancel the event.

MORE INFORMATION

For example, the default action of pressing the TAB key on an HTML page is to move the caret to the next item in the tabindex. With Internet Explorer 4, you could set the focus to whichever element you wanted without setting the event object's returnValue. In Internet Explorer 5 you need to set it. This is also the best way to cancel a form submission in Internet Explorer 4 and 5.

To cancel the default action, you need to set window.event.returnValue=false inside your event.

The following code demonstrates how to cancel the default action. Place this code in an HTML file and view in Internet Explorer.

<HTML> 
<HEAD> 
<SCRIPT LANGUAGE=vbscript> 
<!-- 

Sub text1_onkeydown 
  Select Case window.event.keyCode 
    Case 9   '-Tab
    'Please uncomment this line to cancel the default action
        'window.event.returnValue = false 
    button1.focus 
    Case Else 
    End Select     
End Sub 
--> 
</SCRIPT> 
</HEAD> 

<BODY bgcolor=paleturquoise> 

<P> 
When pressing <TAB> while in the text box in Internet Explorer 5, the 
next text box is getting the focus even though the code directs the focus 
to the button.
</P> 
<P>
You need to cancel the event to override the default action (which, in 
this case, is to set focus to the next item in the tabindex). Merely 
overriding the event and setting focus elsewhere does not prevent the 
default action in Internet Explorer 5. To cancel the default behavior, you 
need to  set <EM>window.event.returnValue = false</EM> inside your event.

</P>

<P> 
Text1 
<INPUT id=text1 name=text1 tabindex=1>
Text2 
<INPUT id=text2 name=text2 tabindex=2></P> 

<P> 
Button 
<INPUT id=button1 name=button1 type=button value=Button tabindex=3> 
</P> 
</BODY> 
</HTML> 
                

REFERENCES

For more information, see the following article in the Microsoft Knowledge Base:

216435 INFO: Validate Data when Submitting Forms with Internet Explorer


For more information about developing Web-based solutions for Microsoft Internet Explorer, visit the following Microsoft Web sites:


Additional query words: IE4 IE5 change VBScript focus

Keywords: kbinfo kbdhtml KB231322