Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > Why won't the following work in FireFox?

Reply
Thread Tools

Why won't the following work in FireFox?

 
 
Nathan Sokalski
Guest
Posts: n/a
 
      10-17-2007
I have the following code which allows you to drag a div in IE, and have it
then move back to it's natural position when you release the mouse button:


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head><script type="text/javascript">var draggable=0;</script></head>
<body>
<div id="div1"
style="width:300px;height:300px;background-colorrange;"></div>
<div id="div2" style="width:300px;height:300px;background-color:green;"
onmousedown="draggable=1;this.style.position='abso lute';this.style.left=event.x-25+'px';this.style.top=event.y-25+'px';"
onmousemove="if(draggable==1){this.style.left=even t.x-25+'px';this.style.top=event.y-25+'px';}"
onmouseup="draggable=0;this.style.position='static ';"></div>
</body>
</html>


I have written what I would expect to do the same thing in other browsers (I
replaced event.x/event.y with event.layerX/event.layerY). Here is that code:


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head><script type="text/javascript">var draggable=0;</script></head>
<body>
<div id="div1"
style="width:300px;height:300px;background-colorrange;"></div>
<div id="div2" style="width:300px;height:300px;background-color:green;"
onmousedown="draggable=1;this.style.position='abso lute';this.style.left=event.layerX-25+'px';this.style.top=event.layerY-25+'px';"
onmousemove="if(draggable==1){this.style.left=even t.layerX-25+'px';this.style.top=event.layerY-25+'px';}"
onmouseup="draggable=0;this.style.position='static ';"></div>
</body>
</html>


However, when I attempt to drag the div in FireFox it just sort of "jumps
around", it definitely isn't doing what the IE version did in IE. did I do
something wrong? Was there something that should have been in both of the
versions that I forgot? Any help would be appreciated. Thanks.
--
Nathan Sokalski

http://www.nathansokalski.com/


 
Reply With Quote
 
 
 
 
dNagel
Guest
Posts: n/a
 
      10-17-2007
heres my version ...


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<script type="text/javascript">
var draggable=0;

function onMDown(e) {
e = e || window.event;
var el = e.target || e.srcElement;
draggable=1;
el.style.position='absolute';
el.style.left=( e.x || e.clientX )-25+'px';
el.style.top=( e.y || e.clientY )-25+'px';
}

function onMMove(e) {
e = e || window.event;
var el = e.target || e.srcElement;
if(draggable==1){
el.style.left=( e.x || e.clientX )-25+'px';
el.style.top=( e.y || e.clientY )-25+'px';
}
}

function onMUp(e) {
e = e || window.event;
var el = e.target || e.srcElement;
draggable=0;
el.style.position='static';
}
</script>
</head>
<body>
<div
id="div1"
style="width:300px;height:300px;background-colorrange;">
</div>
<div
id="div2"
style="width:300px;height:300px;background-color:green;"
onMouseDown="onMDown(event)"
onMouseMove="onMMove(event)"
onMouseUp="onMUp(event)">
</div>
</body>
</html>



D.
 
Reply With Quote
 
 
 
 
David Mark
Guest
Posts: n/a
 
      10-17-2007
On Oct 16, 9:13 pm, "Nathan Sokalski" <njsokal...@hotmail.com> wrote:
> I have the following code which allows you to drag a div in IE, and have it
> then move back to it's natural position when you release the mouse button:
>
> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
> "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">


Why are you using XHTML transitional for a new document? And why use
XHTML at all if you are going to serve this as HTML?

> <html>
> <head><script type="text/javascript">var draggable=0;</script></head>
> <body>
> <div id="div1"
> style="width:300px;height:300px;background-colorrange;"></div>
> <div id="div2" style="width:300px;height:300px;background-color:green;"
> onmousedown="draggable=1;this.style.position='abso lute';this.style.left=eve*nt.x-25+'px';this.style.top=event.y-25+'px';"


Don't use the x/y properties. They are non-standard and unreliable.
And what is the significance of 25?

> onmousemove="if(draggable==1){this.style.left=even t.x-25+'px';this.style.to*p=event.y-25+'px';}"
> onmouseup="draggable=0;this.style.position='static ';"></div>
> </body>
> </html>
>
> I have written what I would expect to do the same thing in other browsers (I
> replaced event.x/event.y with event.layerX/event.layerY). Here is that code:


Using layerX/layerY is also a bad idea. Use pageX/Y if defined and
clientX/Y otherwise. For the latter pair you must add the scroll
position of the document. You will find that most browsers support
one pair or the other (or both.) If neither is defined, you should
skip the drag and drop operation as the results will be unpredictable.

 
Reply With Quote
 
Mark Rae [MVP]
Guest
Posts: n/a
 
      10-17-2007
"David Mark" <> wrote in message
news: ups.com...

[cross-posting removed]

> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
> "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">


Why are you using XHTML transitional for a new document?

??? Standards compliance, maybe...?

> And why use XHTML at all if you are going to serve this as HTML?


???


--
Mark Rae
ASP.NET MVP
http://www.markrae.net

 
Reply With Quote
 
David Mark
Guest
Posts: n/a
 
      10-17-2007
On Oct 16, 11:44 pm, dNagel <NOTGrandNa...@NotMail.com> wrote:
> heres my version ...
>


I'm not sure what happened to my reply to this thread.

> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
> "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
> <html>
> <head>
> <script type="text/javascript">
> var draggable=0;
>
> function onMDown(e) {
> e = e || window.event;


You don't need this line. Your inline handler passed the event
object.

> var el = e.target || e.srcElement;


You wouldn't need this line if you passed "this" as a second
parameter.

> draggable=1;
> el.style.position='absolute';
> el.style.left=( e.x || e.clientX )-25+'px';


As I mentioned in my missing post, you shouldn't use the x/y pair.
Use pageX/Y if defined, then try clientX/Y and then give up as x/y and
layerX/layerY are unpredictable. This works reliably on virtually
every modern browser (and many older ones too.)

There are a few problems with the above line of code. For one, your
test is off. If e.x is 0 and e.clientX is undefined, you will end up
with NaNpx. Same if e.x and e.clientX are both undefined. Then there
is the scroll position of the document to deal with (clientX/Y returns
coordinates relative to the viewport.)

I don't see the significance of 25 either, but that is from the
original.

> el.style.top=( e.y || e.clientY )-25+'px';


Same problems here.

[snip]

> <div
> id="div2"
> style="width:300px;height:300px;background-color:green;"
> onMouseDown="onMDown(event)"
> onMouseMove="onMMove(event)"
> onMouseUp="onMUp(event)">


You don't want the mouseup or mousemove listeners attached to just the
dragged element. They need to be attached to the document.

 
Reply With Quote
 
Nathan Sokalski
Guest
Posts: n/a
 
      10-17-2007
I am using XHTML because the place where I will be using these techniques is
in an ASP.NET page, which is created using that header. The x/y properties
are only used in the IE version; both pageX/Y and clientX/Y gave an alert
saying they were undefined, so I did not know what else to use for IE. Is
there something else that works in IE? I would like to skip the drag & drop,
but my boss won't let me.
--
Nathan Sokalski

http://www.nathansokalski.com/

"David Mark" <> wrote in message
news: ups.com...
On Oct 16, 9:13 pm, "Nathan Sokalski" <njsokal...@hotmail.com> wrote:
> I have the following code which allows you to drag a div in IE, and have
> it
> then move back to it's natural position when you release the mouse button:
>
> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
> "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">


Why are you using XHTML transitional for a new document? And why use
XHTML at all if you are going to serve this as HTML?

> <html>
> <head><script type="text/javascript">var draggable=0;</script></head>
> <body>
> <div id="div1"
> style="width:300px;height:300px;background-colorrange;"></div>
> <div id="div2" style="width:300px;height:300px;background-color:green;"
> onmousedown="draggable=1;this.style.position='abso lute';this.style.left=eve*nt.x-25+'px';this.style.top=event.y-25+'px';"


Don't use the x/y properties. They are non-standard and unreliable.
And what is the significance of 25?

> onmousemove="if(draggable==1){this.style.left=even t.x-25+'px';this.style.to*p=event.y-25+'px';}"
> onmouseup="draggable=0;this.style.position='static ';"></div>
> </body>
> </html>
>
> I have written what I would expect to do the same thing in other browsers
> (I
> replaced event.x/event.y with event.layerX/event.layerY). Here is that
> code:


Using layerX/layerY is also a bad idea. Use pageX/Y if defined and
clientX/Y otherwise. For the latter pair you must add the scroll
position of the document. You will find that most browsers support
one pair or the other (or both.) If neither is defined, you should
skip the drag and drop operation as the results will be unpredictable.


 
Reply With Quote
 
Randy Webb
Guest
Posts: n/a
 
      10-17-2007
Nathan Sokalski said the following on 10/17/2007 12:12 PM:
> I am using XHTML because the place where I will be using these techniques is
> in an ASP.NET page, which is created using that header.


Nice to know Microsoft products generate a document that a Microsoft
browser can't handle.

--
Randy
Chance Favors The Prepared Mind
comp.lang.javascript FAQ - http://jibbering.com/faq/index.html
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
 
Reply With Quote
 
David Mark
Guest
Posts: n/a
 
      10-18-2007
On Oct 17, 12:12 pm, "Nathan Sokalski" <njsokal...@hotmail.com> wrote:
> I am using XHTML because the place where I will be using these techniques is
> in an ASP.NET page, which is created using that header. The x/y properties


That seems an odd choice for a Microsoft product.

> are only used in the IE version; both pageX/Y and clientX/Y gave an alert
> saying they were undefined, so I did not know what else to use for IE. Is


The clientX/Y properties are certainly present in IE.

> there something else that works in IE? I would like to skip the drag & drop,
> but my boss won't let me.


There is some very simple cross-browser drag and drop code in this
project:

http://code.google.com/p/niceshowcase/

You will see that it only uses pageX/Y and clientX/Y.

 
Reply With Quote
 
dNagel
Guest
Posts: n/a
 
      10-18-2007


>> e = e || window.event;
>>

> You don't need this line. Your inline handler passed the event
> object.
>

You do need it... event is not valid in firefox, so it will pass
undefined instead of what you think
should be the event object. FFox always passes the event object into
the into an event procedure.
If you do not use my code exactly as I have written it, it will fail. I
promise you that.

>> var el = e.target || e.srcElement;
>>

>
> You wouldn't need this line if you passed "this" as a second
> parameter.
>
>

Why pass it as a parameter when the event object already has it as a
property.
>> draggable=1;
>> el.style.position='absolute';
>> el.style.left=( e.x || e.clientX )-25+'px';
>>

>
> As I mentioned in my missing post, you shouldn't use the x/y pair.
> Use pageX/Y if defined, then try clientX/Y and then give up as x/y and
> layerX/layerY are unpredictable. This works reliably on virtually
> every modern browser (and many older ones too.)
>
>

sounds good to me.

> There are a few problems with the above line of code. For one, your
> test is off. If e.x is 0 and e.clientX is undefined, you will end up
> with NaNpx. Same if e.x and e.clientX are both undefined. Then there
> is the scroll position of the document to deal with (clientX/Y returns
> coordinates relative to the viewport.)
>
> I don't see the significance of 25 either, but that is from the
> original.
>
>

agreed, but what it does is "position the cursor" a bit inside of the
object so that accelerated mouse
movement does not allow the mouse to escape the "capture".

>> el.style.top=( e.y || e.clientY )-25+'px';
>>

>
> Same problems here.
>
>

agreed.

> [snip]
>
>
>> <div
>> id="div2"
>> style="width:300px;height:300px;background-color:green;"
>> onMouseDown="onMDown(event)"
>> onMouseMove="onMMove(event)"
>> onMouseUp="onMUp(event)">
>>

>
> You don't want the mouseup or mousemove listeners attached to just the
> dragged element. They need to be attached to the document.
>
>

sounds good to me too...

I have not tried it so I'll take your word for it.



D.
 
Reply With Quote
 
David Mark
Guest
Posts: n/a
 
      10-18-2007
On Oct 17, 11:30 pm, dNagel <NOTGrandNa...@NotMail.com> wrote:
> >> e = e || window.event;

>
> > You don't need this line. Your inline handler passed the event
> > object.

>
> You do need it... event is not valid in firefox, so it will pass


Wrong.

> undefined instead of what you think


If that were true, your code would try to use window.event, which
certainly isn't valid in FF.

> should be the event object. FFox always passes the event object into
> the into an event procedure.


You need to re-read your code.

> If you do not use my code exactly as I have written it, it will fail. I
> promise you that.


You can promise anything you want. You are still incorrect.

>
> >> var el = e.target || e.srcElement;

>
> > You wouldn't need this line if you passed "this" as a second
> > parameter.

>
> Why pass it as a parameter when the event object already has it as a
> property.


It is simpler to do it that way. The resulting code is smaller as
well.

>> draggable=1;
> >> el.style.position='absolute';
> >> el.style.left=( e.x || e.clientX )-25+'px';

>
> > As I mentioned in my missing post, you shouldn't use the x/y pair.
> > Use pageX/Y if defined, then try clientX/Y and then give up as x/y and
> > layerX/layerY are unpredictable. This works reliably on virtually
> > every modern browser (and many older ones too.)

>
> sounds good to me.
>
> > There are a few problems with the above line of code. For one, your
> > test is off. If e.x is 0 and e.clientX is undefined, you will end up
> > with NaNpx. Same if e.x and e.clientX are both undefined. Then there
> > is the scroll position of the document to deal with (clientX/Y returns
> > coordinates relative to the viewport.)

>
> > I don't see the significance of 25 either, but that is from the
> > original.

>
> agreed, but what it does is "position the cursor" a bit inside of the
> object so that accelerated mouse
> movement does not allow the mouse to escape the "capture".


That is a pretty unreliable approach and will make the element jump
every time you drag it. Just attach the onmousemove and onmouseup
handlers to the document.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
The Web server reported the following error when attempting to create or open the Web project located at the following URL: 'http://localhost/822319ev1'. 'HTTP/1.1 500 Internal Server Error'. chanmm ASP .Net 2 09-07-2010 07:37 AM
why why why why why Mr. SweatyFinger ASP .Net 4 12-21-2006 01:15 PM
findcontrol("PlaceHolderPrice") why why why why why why why why why why why Mr. SweatyFinger ASP .Net 2 12-02-2006 03:46 PM
why the following HereDoc print don't work? ÕÔ±û·å Perl Misc 29 12-20-2004 11:33 AM
RE: The Web server reported the following error when attempting to create or open the Web project located at the following URL: <URL> =?Utf-8?B?VHJldm9yIEJlbmVkaWN0IFI=?= ASP .Net 0 06-07-2004 07:36 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57