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
Solved

How do I attach event handlers in IE?

Posted on 2011-02-10
8
842 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
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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.

 

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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
html input 8 42
Head to not include on scroll 4 23
Page auto-refreshes when scrolled on iPhone. 5 55
2 separate CSS animations 2 17
Introduction Knockoutjs (Knockout) is a JavaScript framework (Model View ViewModel or MVVM framework).   The main ideology behind Knockout is to control from JavaScript how a page looks whilst creating an engaging user experience in the least …
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

838 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