Solved

Javascript events ondeactivate in non IE

Posted on 2016-09-09
1
44 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 21

Accepted Solution

by:
Kim Walker earned 500 total points
Comment Utility
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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

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 will give core knowledge of JavaScript and will head in to your first JavaScript program. I am Durvesh Naik and I am here to deal with this series of JavaScript. I will teach you JavaScript in part wise , as its quite boring to read big…
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…

744 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now