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

ID: Q231322


The information in this article applies to:


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 &lt;TAB&gt; 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:

Q216435 INFO: Validate Data when Submitting Forms with Internet Explorer
For more information, please see the MSDN Web Workshop:
http://msdn.microsoft.com/workshop/default.asp

Additional query words: IE4 IE5 change VBScript focus


Keywords          : kbDHTML kbIE400 kbIE401 kbVBScript kbIE401sp1 kbIE401sp2 kbGrpInet kbIE500 
Version           : WINDOWS:4.0,4.01,4.01 SP1,4.01 SP2,5.0
Platform          : WINDOWS 
Issue type        : kbinfo 

Last Reviewed: May 27, 1999