Jump to content

Document OnBlur/OnFocus


miffe

Recommended Posts

Greetings,I was asked to cancel the backspace function for going back a page in history, so I simply catched and displayed an error when document.onkeydown was 8... though this also applies for textboxes and other objects.I could catch every key event for every text box, though this would mean modifying 196 pages, so I prefered to add it to my general file for JS functions which loads in every page. I am trying to catch the documentĀ“s selected object name, so if it is the website it will show an error if backspace is pressed, but if it is a textbox it will allow it.So far I've tried:

var focdocument.onfocus = varset();document.onblur = varunset();function varset(){foc = 1return true;}function varunset(){foc=0return false;}if ((event.KeyCode==8)){alert ("Focus: " + foc);}

Obviously this doesn't work, I can't find the correct method of doing this to affect all pages in this general function. Can anyone help me?Thanks,miffe

Link to comment
Share on other sites

I think the document is always focused, as long as the window is. Instead of changing the focus in the events in the document, use document.getElementsByTagName to find all input and textarea tags and use the event handlers for those tags to say whether or not an input has focus. You're really only concerned about input and textarea tags, so attach the event handler to those instead of the document. Also, always return true in the event handler unless you want to cancel the event.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...