Solved

How do I attach event handlers in IE?

Posted on 2011-02-10
8
830 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
Comment Utility
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
Comment Utility
check this link
http://www.quirksmode.org/js/events_advanced.html

especially the last part of it
0
 

Author Comment

by:chaseivey
Comment Utility
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
 

Author Comment

by:chaseivey
Comment Utility
OOPS!  Sorry....wrond lines.    look at lines 43-44
0
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
LVL 40

Expert Comment

by:gurvinder372
Comment Utility
ok, remove lines 43, 44 and try again
0
 

Author Comment

by:chaseivey
Comment Utility
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
Comment Utility
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
Comment Utility
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Shoutout to Emily Plummer (http://www.experts-exchange.com/members/eplummer26.html) for giving me this article! She did most of it, I just finished it up and posted it for her :)    Introduction In a previous article (http://www.experts-exchang…
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
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 …

771 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