Solved

Javascript events ondeactivate in non IE

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Call a function within the ASP code 4 19
Underline in nav for the page that you are currently on. 1 22
Insert Button on a table 16 38
Autocomplete with Jquery Question 2 20
This article shows how to create and access 2-dimensional arrays in JavaScript.  It includes a tutorial in case you are just trying to "get your head wrapped around" the concept and we'll also look at some useful tips for more advanced programmers. …
Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
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…

828 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