Notice: This website is an unofficial Microsoft Knowledge Base (hereinafter KB) archive and is intended to provide a reliable access to deleted content from Microsoft KB. All KB articles are owned by Microsoft Corporation. Read full disclaimer for more details.

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


View products that this article applies to.

This article was previously published under Q231322

↑ Back to the top


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.

↑ Back to the top


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> 
				

↑ Back to the top


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:

↑ Back to the top


Keywords: KB231322, kbinfo, kbdhtml

↑ Back to the top

Article Info
Article ID : 231322
Revision : 3
Created on : 5/11/2006
Published on : 5/11/2006
Exists online : False
Views : 352