Solved

Javascript events ondeactivate in non IE

Posted on 2016-09-09
1
60 Views
Last Modified: 2016-09-09
I use the following code as follows:

document.onclick = documentClickHandler;
document.ondblclick = documentDoubleClickHandler;
document.ondeactivate = documentDeactivateHandler;
document.onchange = documentChangeHandler;

Then I have a function for each such as the following

function documentDeactivateHandler(ev)  
{
      
ev = ev || window.event; var target = ev.target  || ev.srcElement;
      
      
      if($(target).blahblah then) {//dosomething}

}

All good, except users are starting to use Chrome and the ondeactivate does not work in Chrome.
Some google searches reveals ondeactivate is a Microsoft thing, so tried onblur, still did not work.

Two parts to the question:
1.  Is this a good approach to trapping events and processing them?
2.  How do I adapt to all browser types/

THANKS!
0
Comment
Question by:maxdbase
1 Comment
 
LVL 22

Accepted Solution

by:
Kim Walker earned 500 total points
ID: 41792214
Based on my research, the IE-only ondeactivate event fires immediately before the onblur event. Since the ondeactivate event is not recognized by other browsers, the onblur event should occur at the same point as the ondeactivate event in IE. So there's no reason that onblur would not work.

However, the onchange event is an onblur event that only fires when the contents of the input field have changed. So a change and blur event handler on the same element would compete with one another and may be causing one or the other to fail.

I would recommend you replace both event handlers with a single onblur that includes a conditional that checks to see if the contents of the field have changed.
<input type="text" name"testme" id="testme" value="test value" data-original-value="test value" onblur="check_for_change(this);">

Open in new window

Since you have not specified jQuery in your tags, I will not bore you with jQuery code. Here's the raw javascript code.
function check_for_change(e) {
	var cur_val = e.value;
	var orig_val = e.getAttribute('data-original-value');
	if (cur_val === orig_val) {
		// values are the same
		// perform normal onblur functions
	} else {
		// values are NOT the same
		// remember new value
		e.setAttribute('data-original-value',cur_val);
		// perform onchange functions
	}
}

Open in new window

0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this article, we'll look how to sort an Array in JavaScript, including the more advanced techniques of sorting a collection of records either ascending or descending on two or more fields. Basic Sorting of Arrays First, let's look at the …
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question