Results 1 to 6 of 6

Thread: firefox issue

  1. #1
    Join Date
    Jul 2005
    Location
    Canada
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default firefox issue

    Hi all.
    In a nutshell, I have a page with colour swatches.
    I have implemented some JS and CSS in order for a user to "move" the swatches around so they can take different colours and move them next to each other in order to see how they look side by side on the site.

    I reference the js in the head, and call it in a CSS class.
    works in IE but not in Firefox.
    any help?

    JS file:
    Code:
    var ie=document.all;
    var nn6=document.getElementById&&!document.all;
    var isdrag=false;
    var x,y;
    var dobj;
    
    function movemouse(e)
    {
      if (isdrag)
      {
        dobj.style.left = nn6 ? tx + e.clientX - x : tx + event.clientX - x;
        dobj.style.top  = nn6 ? ty + e.clientY - y : ty + event.clientY - y;
        return false;
      }
    }
    
    function selectmouse(e)
    {
      var fobj       = nn6 ? e.target : event.srcElement;
      var topelement = nn6 ? "HTML" : "BODY";
      while (fobj.tagName != topelement && fobj.className != "dragme")
      {
        fobj = nn6 ? fobj.parentNode : fobj.parentElement;
      }
      if (fobj.className=="dragme")
      {
        isdrag = true;
        dobj = fobj;
        tx = parseInt(dobj.style.left+0,10);
        ty = parseInt(dobj.style.top+0,10);
        x = nn6 ? e.clientX : event.clientX;
        y = nn6 ? e.clientY : event.clientY;
        document.onmousemove=movemouse;
        return false;
      }
    }
    document.onmousedown=selectmouse;
    document.onmouseup=new Function("isdrag=false");
    HTML head:
    Code:
    <script type="text/javascript" src="dragAndDrop.js"></script>
    body CSS call:
    Code:
    <img src="Images/colours/standard_colours/polar_white.png" alt="Polar White" width="114" height="75" class="dragme" />
    IE 7 and 8 work fine, FF 3.5.14 and 3.6.13 do not work.

    appreciate the help and understanding,
    thanks,
    Tin
    Last edited by jscheuer1; 04-12-2011 at 10:18 AM. Reason: format code

  2. #2
    Join Date
    Dec 2008
    Location
    Portsmouth, UK
    Posts
    1,891
    Thanks
    2
    Thanked 441 Times in 435 Posts

    Default

    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
    
    <head>
      <title></title>
    <script type="text/javascript">
    /*<![CDATA[*/
    var ie=document.all;
    var nn6=document.getElementById&&!document.all;
    var isdrag=false;
    var x,y;
    var dobj;
    
    function movemouse(e)
    {
    if (isdrag)
    {
    dobj.style.left = (nn6 ? tx + e.clientX - x : tx + event.clientX - x)+'px';
    dobj.style.top = (nn6 ? ty + e.clientY - y : ty + event.clientY - y)+'px';
    return false;
    }
    }
    
    function selectmouse(e){
     var fobj = e.target||event.srcElement;
     var topelement = nn6 ? "HTML" : "BODY";
     while (fobj.tagName != topelement && fobj.className != "dragme"){
      fobj = nn6 ? fobj.parentNode : fobj.parentElement;
     }
     if (fobj.className=="dragme"){
      isdrag = true;
      dobj = fobj;
      tx = parseInt(dobj.style.left+0,10);
      ty = parseInt(dobj.style.top+0,10);
      x = nn6 ? e.clientX : event.clientX;
      y = nn6 ? e.clientY : event.clientY;
      document.onmousemove=movemouse;
      return false;
     }
    }
    document.onmousedown=selectmouse;
    document.onmouseup=new Function("isdrag=false");
    
    /*]]>*/
    </script></head>
    
    <body>
    <img src="http://www.vicsjavascripts.org.uk/StdImages/One.gif" alt="Polar White" width="114" height="75" class="dragme" style="position:absolute;left:100px;top:100px;"/>
    
    </body>
    
    </html>
    Vic
    God Loves You and will never love you less.
    http://www.vicsjavascripts.org/Home.htm
    If my post has been useful please donate to http://www.operationsmile.org.uk/

  3. #3
    Join Date
    Jul 2005
    Location
    Canada
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Thanks Vic

    Not exactly sure why the small changes worked, thank you.
    I would appreciate the understanding if anyone wants to bother.

    But thank you again vwphillips

  4. #4
    Join Date
    Jul 2005
    Location
    Canada
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Thank you.
    I would appreciate the understanding if anyone wants to bother.

    alright, I made the changes, then it would work in firefox, but not in IE
    so I kept this change:
    Code:
    function movemouse(e)
    {
    if (isdrag)
    {
    dobj.style.left = (nn6 ? tx + e.clientX - x : tx + event.clientX - x)+'px';
    dobj.style.top = (nn6 ? ty + e.clientY - y : ty + event.clientY - y)+'px';
    return false;
    }
    }
    but this edit seems to disable IE from working:
    Code:
    function selectmouse(e)
    {
    function selectmouse(e){
     var fobj = e.target||event.srcElement;
     var topelement = nn6 ? "HTML" : "BODY";



    But thank you again vwphillips
    Last edited by jscheuer1; 04-12-2011 at 10:19 AM. Reason: format code

  5. #5
    Join Date
    Jul 2005
    Location
    Canada
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Bug in IE8

    Ok, I just noticed a bug.
    Using IE V.8 the scrollbars do not work. I can scroll with my mouse wheel, but not the browser's scroll bar.
    Here's the code:

    Code:
    var ie=document.all;
    var nn6=document.getElementById&&!document.all;
    var isdrag=false;
    var x,y;
    var dobj;
    
    function movemouse(e)
    {
      if (isdrag)
      {
        dobj.style.left = (nn6 ? tx + e.clientX - x : tx + event.clientX - x)+'px';
    
        dobj.style.top  = (nn6 ? ty + e.clientY - y : ty + event.clientY - y)+'px';
        return false;
      }
    }
    
    function selectmouse(e)
    {
      var fobj       = nn6 ? e.target : event.srcElement;
      var topelement = nn6 ? "HTML" : "BODY";
      while (fobj.tagName != topelement && fobj.className != "dragme")
      {
        fobj = nn6 ? fobj.parentNode : fobj.parentElement;
      }
      if (fobj.className=="dragme")
      {
        isdrag = true;
        dobj = fobj;
        tx = parseInt(dobj.style.left+0,10);
        ty = parseInt(dobj.style.top+0,10);
        x = nn6 ? e.clientX : event.clientX;
        y = nn6 ? e.clientY : event.clientY;
        document.onmousemove=movemouse;
        return false;
      }
    }
    document.onmousedown=selectmouse;
    document.onmouseup=new Function("isdrag=false");

    Not a clue as to why this is happening, would appreciate some help.
    Thanks,
    Tin.
    Last edited by jscheuer1; 04-12-2011 at 10:20 AM. Reason: format code

  6. #6
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    Code:
    <!DOCTYPE html>
    <html>
    <head>
    <title></title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <script type="text/javascript">
    (function(){
    	var isdrag = false, x, y, dobj;
    
    	function movemouse(e)
    	{
    	  e = e || event;
    	  if (isdrag)
    	  {
    	    dobj.style.left = tx + e.clientX - x + 'px';
    	    dobj.style.top  = ty + e.clientY - y + 'px';
    	    return false;
    	  }
    	}
    
    	function selectmouse(e)
    	{
    	  e = e || event;
    	  var fobj = e.target || e.srcElement;
    	  while (fobj.parentNode && fobj.className !== "dragme")
    	  {
    	    fobj = fobj.parentNode;
    	  }
    	  if (fobj.className === "dragme")
    	  {
    	    isdrag = true;
    	    dobj = fobj;
    	    tx = parseInt(dobj.style.left || dobj.offsetLeft, 10);
    	    ty = parseInt(dobj.style.top || dobj.offsetTop, 10);
    	    x = e.clientX;
    	    y = e.clientY;
    	    document.onmousemove = movemouse;
    	    return false;
    	  }
    	}
    	document.onmousedown = selectmouse;
    	document.onmouseup = function(){isdrag = false;};
    })();
    </script>
    <style type="text/css">
    .dragme {
    	position:absolute;
    }
    </style>
    </head>
    <body>
    <p>&nbsp;</p>
    <p>&nbsp;</p>
    <p>&nbsp;</p>
    <img src="Images/colours/standard_colours/polar_white.png" alt="Polar White" width="114" height="75" class="dragme">
    <p>&nbsp;</p>
    <p>&nbsp;</p>
    <p>&nbsp;</p>
    <p>&nbsp;</p>
    <p>&nbsp;</p>
    <p>&nbsp;</p>
    <p>&nbsp;</p>
    <p>&nbsp;</p>
    <p>&nbsp;</p>
    <p>&nbsp;</p>
    <p>&nbsp;</p>
    <p>&nbsp;</p>
    <p>&nbsp;</p>
    <p>&nbsp;</p>
    <p>&nbsp;</p>
    <p>&nbsp;</p>
    <p>&nbsp;</p>
    <p>&nbsp;</p>
    <p>&nbsp;</p>
    <p>&nbsp;</p>
    <p>&nbsp;</p>
    <p>&nbsp;</p>
    <p>&nbsp;</p>
    </body>
    </html>
    Notes:
    • Why browser detect when you can feature detect? Checking for document.all is no guarantee that you have an IE browser. And the assumptions (most notably the parentElement object, and picking an arbitrary topelement by perceived browser type) the original script made about IE haven't been entirely true since I believe IE version 4, which almost no one uses. We can safely drop support for it. The version of the code in this post works in IE 5 through and including IE 9, as well as Firefox, probably all modern browsers.

    • Why expose your variables to the global scope? In this case, by enclosing the code in a self executing function all variables become local.

    • The scrollbar problem was (best I can determine) that when you mousedown on the scrollbar using the antiquated parentElement object and the arbitrary topelement, the script threw an error. I changed to check to see if there was a parentNode. That way, if there is no perceived parentNode, it stops looking without checking for class or tag names on something that might not exist or might not have them.

    • I separated style from content by adding the initial check for the element's offsetLeft/Top in lieu of specified inline style values. Inline style may still be used if desired for the initial positioning. Or the element may be given an id to set it's left and top coordinates in a stylesheet. Using the offsets as I have might not work in some browsers if the dragme element is not a direct descendant (immediate childNode) of the body.
    Last edited by jscheuer1; 04-12-2011 at 12:20 PM. Reason: specificity in notes
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •