Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Javascript events ondeactivate in non IE

Posted on 2016-09-09
1
Medium Priority
?
82 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

Article by: DanRollins
This article describes a JavaScript program that creates a maze made of hexagonal cells.  In Part 2 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7850-Hex-Maze-Part-2.html), we'll extend the program by adding a depth-…
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…

730 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