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?
 
Sarika30Connect With a Mentor Commented:
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
 
Michel PlungjanConnect With a Mentor IT 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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.