Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

How do I attach event handlers in IE?

Posted on 2011-02-10
8
Medium Priority
?
880 Views
Last Modified: 2012-06-21
Hello.

I have some JS code that is working great in firefox, but for some reason it fails in IE.
I think the problem may be with the way I'm attaching the events. Can someone look at this code and tell me what's wrong.

There is a live example at rdastore.com/tester.cfm.
Just select something from the pulldown menu and the form should submit.
Like I said, works great in FF, but not in IE.

All this code does is attach simple form submissions and such to 'click' and 'change' events, so it should be fairly repetitive and easy to read.
0
Comment
Question by:chaseivey
[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
  • 6
  • 2
8 Comments
 

Author Comment

by:chaseivey
ID: 34864174
Crap.  Forgot the code.  Here it is. :)
window.onLoad = initAll();

function initAll() {
	
	function confirmDelete() {
	
		var dels = new Array();	
		var del1 = document.getElementsByName('delete');
		for (var i=0;i<del1.length;i++) {
			dels.push(del1[i]);
		}
		/* having trouble attaching mutiple events to the same element
		var del2 = document.getElementsByName('noCard');		
		for (var i=0;i<del2.length;i++) {
			dels.push(del2[i]);
		}
		*/
		
		//Cross-browser event handler
		function callback(evt) {
			evt = evt || window.event;
			var target = (typeof evt.target !== 'undefined') ? evt.target : evt.srcElement;
			//Actual work here
			if (evt.returnValue) {//IE
				if (!confirm('Are you sure you want to delete?')) {
					evt.returnValue = false;
				}
				evt.cancelBubble = true;
				
			} else if (evt.preventDefault) {//FF
				if (!confirm('Are you sure you want to delete?')) {
					evt.preventDefault();
				}
				evt.stopPropagation();
					
			}
		}
		
		for (var i = 0; i < dels.length; i++) {
		
			if (document.addEventListener) {//FF
				dels[i].addEventListener('click', callback, false);
			} else if (document.attachEvent) {//IE
				dels[i].attachEvent('onclick', callback);
			} else {//other
				dels[i].onclick = callback;
			}
		}
	}
	confirmDelete();
	
	function changeSubmit() {
		
		var chgs = document.getElementsByClassName('changeSubmit');
		
		//Cross-browser event handler
		function callback(evt) {
			evt = evt || window.event;
			var target = (typeof evt.target !== 'undefined') ? evt.target : evt.srcElement;
			//Actual work here
			this.form.submit();			
			if (evt.returnValue) {//IE
				evt.cancelBubble = true;
			} else if (evt.preventDefault) {//FF
				evt.stopPropagation();
			}
		}
		
		for (var i=0; i < chgs.length; i++) {
			if (document.addEventListener) {//FF
				chgs[i].addEventListener('change',callback,false);
			} else if (document.attachEvent) {//IE
				chgs[i].attachEvent('onchange',callback);
			} else {
				chgs[i].onchange = callback;
			}
		}
		
	}
	changeSubmit();
	
	function clickSubmit() {
		
		var clks = document.getElementsByClassName('clickSubmit');
		
		//Cross-browser event handler
		function callback(evt) {
			evt = evt || window.event;
			var target = (typeof evt.target !== 'undefined') ? evt.target : evt.srcElement;
			//Actual work here
			this.form.submit();			
			if (evt.returnValue) {//IE
				evt.cancelBubble = true;
			} else if (evt.preventDefault) {//FF
				evt.stopPropagation();
			}
		}
		
		for (var i=0; i < clks.length; i++) {
			if (document.addEventListener) {//FF
				clks[i].addEventListener('click',callback,false);
			} else if (document.attachEvent) {//IE
				clks[i].attachEvent('onclick',callback);
			} else {
				clks[i].onclick = callback;
			}
		}
		
	}
	clickSubmit();
	
	function blurSubmit() {
		
		var blurs = document.getElementsByClassName('blurSubmit');
		
		//Cross-browser event handler
		function callback(evt) {
			evt = evt || window.event;
			var target = (typeof evt.target !== 'undefined') ? evt.target : evt.srcElement;
			//Actual work here
			this.form.submit();			
			if (evt.returnValue) {//IE
				evt.cancelBubble = true;
			} else if (evt.preventDefault) {//FF
				evt.stopPropagation();
			}
		}
		
		for (var i=0; i < blurs.length; i++) {
			if (document.addEventListener) {//FF
				blurs[i].addEventListener('blur',callback,false);
			} else if (document.attachEvent) {//IE
				blurs[i].attachEvent('onblur',callback);
			} else {
				blurs[i].onblur = callback;
			}
		}
		
	}
	blurSubmit();
}

Open in new window

0
 
LVL 40

Accepted Solution

by:
Gurvinder Pal Singh earned 1000 total points
ID: 34864217
check this link
http://www.quirksmode.org/js/events_advanced.html

especially the last part of it
0
 

Author Comment

by:chaseivey
ID: 34865314
ok.
Checked that link, and I think I'm doing it correctly...

Look at line 33-34 of my code.

Then look at my live example:  rdastore.com/tester2.cfm

when you click the box, it should prompt you for confirmation, then submit the form.
Works in FF, but nothing happens in IE.

Can you see where I'm going wrong?
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

Author Comment

by:chaseivey
ID: 34865327
OOPS!  Sorry....wrond lines.    look at lines 43-44
0
 
LVL 40

Expert Comment

by:Gurvinder Pal Singh
ID: 34867320
ok, remove lines 43, 44 and try again
0
 

Author Comment

by:chaseivey
ID: 34873765
OK.  I have simplified the code to narrow down the issue.
Please see the attached js code.

As you suggested, I tried removing lines 18-19, and nothing happened.

I want the alert 'Success!' to fire whenever the user clicks the checkbox.

Please look at the live page: rdastore.com/tester2.cfm
and see if I'm doing anything wrong.  Feel free to view the page source and see if I'm linking to the js file correctly.

It is still working flawlessly in Firefox and even IE9, but not IE8.
Perhaps there is a small, careless error I'm making somewhere that Firefox is forgiving?

Please advise.
window.onLoad = initAll();

function initAll() {
	
	function confirmDelete() {
		
		var myVar = document.getElementById('me');
		
		//Cross-browser event handler
		function callback(evt) {
			//Actual work here
			alert('SUCCESS!');
					
		}
		
		 	if (document.attachEvent) {//IE
				myVar.attachEvent('onclick', callback);
			} else if (document.addEventListener) {//FF
				myVar.addEventListener('click', callback, false);
			} else {//other
				myVar.onclick = callback;
			}
		
	}
	confirmDelete();

}

Open in new window

0
 

Author Comment

by:chaseivey
ID: 34875202
Ok.  I don't know what happened, but it all of the sudden started working.  :/
I did however find some posts online about IE 8 having some event-handling issues tied to the DOCTYPE declaration.  Not sure if you know anything about that.

I'm gonna give this a couple days of testing before I close the question, just in case it's still broken. :)
0
 

Author Comment

by:chaseivey
ID: 34888458
Ok. l seriously have no idea why the code was giving me so much trouble, but I discovered one thing that seemed to fix it right up for IE 8.

In my callback function, I was first checking for IE, then for Firefox with this:

if (evt.returnValue) {...some IE code here } else if (evt.preventDefault) {...some FF code here }

This would cause the IE code never to fire, so apparently, testing for 'true' on evt.retunValue wasn't the best way to check for IE, as it never registered.   ?

My bandaid fix was to change the above code to this:

if (evt.preventDefault) {...some FF code here} else {...some IE code here}

Now it all works, although I'm still not sure why my conditional statement was wrong.

I also changed my event handler code to check for FF first, like so:
 
                        if (document.addEventListener) {//FF
                        myVar.addEventListener('click', callback, false);
                               }
                        else if (document.attachEvent) {//IE
                        myVar.attachEvent('onclick', callback);
                  } else {//other
                        myVar.onclick = callback;
                  }

I'm not 100% sure which of these changes made the difference (although I think it's the first one).  But anyway, all seems to be well for the moment.
I hate IE.

:)
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Introduction Since I wrote the original article about Handling Date and Time in PHP and MySQL several years ago, it seemed like now was a good time to update it for object-oriented PHP.  This article does that, replacing as much as possible the pr…
JavaScript has plenty of pieces of code people often just copy/paste from somewhere but never quite fully understand. Self-Executing functions are just one good example that I'll try to demystify here.
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

610 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