Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Javascript events ondeactivate in non IE

Posted on 2016-09-09
1
Medium Priority
?
94 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 2000 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Having worked on larger scale sites, we found out that you are bound to look at more scalable solutions to integrating widgets, code snippets or complete applications and mesh them into functional sites, in any given composition. To share some of…
This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
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…
Suggested Courses

877 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