Javascript onmousedown fails in Firefox?

I have a simple drag var called ezdrag I made a while back and it works well enough in all browsers except firefox. For some reason it fails after the first 'drag'..

The first drag goes fine, but then after that the cursor just changes to a 'lockout' icon when you click, and it never triggers the 'onmouseup' event until you click again. I'm assuming one of the fuctions breaks it, but I can't tell what. Does anyone have an idea of why this is happening, or a simple workaround?

Here's a sample page you can copy/paste to a blank html to see what happens, or the ezdrag var is in the code section:

<html>
<body>
<div style='cursor:move;position:absolute;width:100px;height:100px;background:#EEE;border:1px solid #AAA;' onmousedown='ezdrag.start(event, this);'></div>
<script type='text/javascript'>
var ezdrag = {
            objx : 0,
            objy : 0,
            mousex : 0,
            mousey : 0,
            obj : null,
            start : function (event, objin){
                  if (!(event)) event=window.event;
                  ezdrag.obj=objin;
                  document.onmousemove=ezdrag.move;
                  document.onmouseup=ezdrag.stop;
                  coords = ezdrag.findoffset(objin);
                  ezdrag.objx=coords.x;
                  ezdrag.objy=coords.y;
                  ezdrag.mousex=event.clientX;
                  ezdrag.mousey=event.clientY;
            },
            move : function (event){
                  if (!(event)) event=window.event;
                  movedx=event.clientX-ezdrag.mousex;
                  movedy=event.clientY-ezdrag.mousey;
                  ezdrag.obj.style.left=(ezdrag.objx+movedx+document.documentElement.scrollLeft)+"px";
                  ezdrag.obj.style.top=(ezdrag.objy+movedy+document.documentElement.scrollTop)+"px";
            },
            stop : function (){
                  document.onmousemove=null;
                  document.onmouseup=null;
                  ezdrag.obj=null;
            },
            findoffset : function(element){
                  var tempvar = new Object();
                  tempvar.x = parseInt(element.offsetLeft);
                  tempvar.y = parseInt(element.offsetTop);
                  if (element.tagName != "BODY") tempvar.y -= parseInt(element.scrollTop);
                  if (element.tagName != "BODY") tempvar.x -= parseInt(element.scrollLeft);
                  var parent = element.offsetParent;
                  while (parent) {
                        tempvar.x += parseInt(parent.offsetLeft);
                        tempvar.y += parseInt(parent.offsetTop);
                        if (parent.tagName != "BODY") tempvar.y -= parseInt(parent.scrollTop);
                        if (parent.tagName != "BODY") tempvar.x -= parseInt(parent.scrollLeft);
                        parent = parent.offsetParent;
                  }
                  return tempvar;
            }
      };
</script>
</body>
</html>

var ezdrag = {
		objx : 0,
		objy : 0,
		mousex : 0,
		mousey : 0,
		obj : null,
		start : function (event, objin){
			if (!(event)) event=window.event;
			ezdrag.obj=objin;
			document.onmousemove=ezdrag.move;
			document.onmouseup=ezdrag.stop;
			coords = ezdrag.findoffset(objin);
			ezdrag.objx=coords.x;
			ezdrag.objy=coords.y;
			ezdrag.mousex=event.clientX;
			ezdrag.mousey=event.clientY;
		},
		move : function (event){
			if (!(event)) event=window.event;
			movedx=event.clientX-ezdrag.mousex;
			movedy=event.clientY-ezdrag.mousey;
			ezdrag.obj.style.left=(ezdrag.objx+movedx+document.documentElement.scrollLeft)+"px";
			ezdrag.obj.style.top=(ezdrag.objy+movedy+document.documentElement.scrollTop)+"px";
		},
		stop : function (){
			document.onmousemove=null;
			document.onmouseup=null;
			ezdrag.obj=null;
		},
		findoffset : function(element){
			var tempvar = new Object();
			tempvar.x = parseInt(element.offsetLeft);
			tempvar.y = parseInt(element.offsetTop);
			if (element.tagName != "BODY") tempvar.y -= parseInt(element.scrollTop);
			if (element.tagName != "BODY") tempvar.x -= parseInt(element.scrollLeft);
			var parent = element.offsetParent;
			while (parent) {
				tempvar.x += parseInt(parent.offsetLeft);
				tempvar.y += parseInt(parent.offsetTop);
				if (parent.tagName != "BODY") tempvar.y -= parseInt(parent.scrollTop);
				if (parent.tagName != "BODY") tempvar.x -= parseInt(parent.scrollLeft);
				parent = parent.offsetParent;
			}
			return tempvar;
		}
	};

Open in new window

LVL 2
Matt KendallTech / Business owner operatorAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Sarika30Commented:
I think you need to add event listeners. Please see the below link
http://bytes.com/topic/javascript/answers/656157-trouble-setting-onmousedown-firefox
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Michel PlungjanIT ExpertCommented:
0
Matt KendallTech / Business owner operatorAuthor Commented:
Thanks for the comments, guys. I don't want to use the Firefox drag and drop functions since they only work with firefox, and I'm hoping to keep it simple so when I need to change the behavior, I only have to do it in 1 spot.

I also tried adding the event listener in javascript with:

document.getElementById("testbox").addEventListener("mousedown", function(event){ezdrag.start(event, this)}, true);

at the bottom of the javascript and took out the onmousedown (and added id) on the div, and it still did the same thing.
0
Matt KendallTech / Business owner operatorAuthor Commented:
I'm just going to assume this is a bug with Firefox that probably has no direct work around.. I could get around it at least by using the firefox drag functions, but it's not a convenient work around, and it'll be easier in the long run to just let Firefox users deal with the strange, but working dragging, thanks for the input!
0
Matt KendallTech / Business owner operatorAuthor Commented:
Just thought I'd update this in case someone comes across something similar.. adding:
if (window.getSelection) window.getSelection().removeAllRanges();
else if (document.selection) document.selection.empty();

to the bottom of both the move and stop functions fixes it in firefox.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Web Browsers

From novice to tech pro — start learning today.