Solved

How do I attach event handlers in IE?

Posted on 2011-02-10
8
838 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
  • 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:
gurvinder372 earned 250 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
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 

Author Comment

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

Expert Comment

by:gurvinder372
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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Styling your websites can become very complex. Here I'll show how SASS can help you better organize, maintain and reuse your CSS code.
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will the learn the benefit of plain text editors and code an HTML5 based template for use in further tutorials.

813 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

11 Experts available now in Live!

Get 1:1 Help Now