473,383 Members | 1,874 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

Enable drag and drop to the text between <span></span>

Hello, all,
I would like to enable some text between <SPAN
url="http://www.testserver.com/">WORD TO BE DRAGGED </SPAN>. I put some
javascript and it will extract http://www.testserver.com/ from the the span
element when I select the whole text in the SPAN and drag it.

However, I want to drag it without have to select the words between the span
element. The default mouse action will only select the words when i move the
mouse. Can someone point me to the right direction? thanks.

Best Regards,
Jay
Jul 23 '05 #1
9 6761
(REVISION to original question)

I would like to enable some text between <SPAN
url="http://www.testserver.com/">WORD TO BE DRAGGED </SPAN> to be able to be
dragged and dropped.

I put some javascript and it will extract http://www.testserver.com/ from
the the span element when I select the whole text in the SPAN and drag it.
However, I want to drag it without have to select the words between the span
element. The default mouse action will only select the words when i move the
mouse. Can someone point me to the right direction? thanks.

Best Regards,
Jay

"Wang, Jay" <ou**********@fastmail.fm> wrote in message
news:c8*********@news.vanderbilt.edu...
Hello, all,
I would like to enable some text between <SPAN
url="http://www.testserver.com/">WORD TO BE DRAGGED </SPAN>. I put some
javascript and it will extract http://www.testserver.com/ from the the span element when I select the whole text in the SPAN and drag it.

However, I want to drag it without have to select the words between the span element. The default mouse action will only select the words when i move the mouse. Can someone point me to the right direction? thanks.

Best Regards,
Jay

Jul 23 '05 #2
Wang, Jay wrote:
I would like to enable some text between <SPAN
url="http://www.testserver.com/">WORD TO BE DRAGGED </SPAN>. I put some
javascript and it will extract http://www.testserver.com/ from the the span
element when I select the whole text in the SPAN and drag it.


This isn't an easy task, since you'd have to analyze text boundaries and
add/remove nodes in the tree. Ranges can help you, but they're supported
so far only by IE and Mozilla. You'll find an example below, not close
to satisfying, but which should nonetheless illustrate the use of many
range methods.

I've used an alternate click system instead of drag/drop, mostly because
of Mozilla not firing mouseup while dragging, and not offering ways to
prevent the drag (AFAIK).

I've also bumped into a strange bug as of Mozilla 1.7a, which would
require try/catch construct to be dealt with (adding an <input
type="text"> in the script, then clicking on it, fails miserably -
impossible to simply read a single property on the object).

<style type="text/css">
span[url]{background:yellow;}
</style>

<div>
Hello, <span url="foo.html">foo!</span>
You can drag and drop the foo where you want!
</div>

<script type="text/javascript">
window.onload=function(evt){
var d=document,rng;
if(d.body&&d.body.getAttribute&&d.body.nodeType&&
typeof d.body.parentNode!="undefined") {

if(d.body.createTextRange) rng=d.body.createTextRange();
else if(d.createRange) rng=d.createRange();
if(rng){
if(rng.moveToElementText&&rng.pasteHTML&&rng.moveT oPoint ||
rng.selectNode&&rng.insertNode&&rng.createContextu alFragment)
d.onclick=new Function("e","drag(e)");
}

}
}
var drag=(function(){

var dragEl;

function CRng(){
var d=document,rng;
if(d.body.createTextRange) { rng=d.body.createTextRange(); }
else if(d.createRange) { rng=d.createRange(); }
return rng;
}

function MRng(rng, el){
if(rng && el){
if(rng.moveToElementText) { rng.moveToElementText(el); }
else if(rng.selectNode) { rng.selectNode(el); }
}
return rng;
}

function SRng(rng){
if(rng){
if(rng.select) rng.select();
else{
with(this.getSelection()){
removeAllRanges();
addRange(rng);
}
}
}
return rng;
}

function WRng(rng,s){
if(rng){
if(rng.pasteHTML)rng.pasteHTML(s);
else rng.insertNode(rng.createContextualFragment(s));
}
return rng;
}

function hasAscendantNode(node,nodeName){
nodeName=nodeName.toLowerCase();
while(node&&node.nodeName.toLowerCase()!=nodeName)
node=node.parentNode;
return !!node;
}

function gtext(el){
if(typeof el.innerText!="undefined") return el.innerText;
else return (function(elmt){
for(var ii=0,buf=[],c=elmt.childNodes;ii<c.length;ii++){
if(c[ii].nodeType==3)buf.push(c[ii].nodeValue);
else if(c[ii].nodeType==1)buf.push(arguments.callee(c[ii]));
}
return buf.join("");
})(el);
}

return function(evt){
var tgt,rng,s,el;
evt=evt||window.event;

if(dragEl){
rng=CRng();
s="<a href='"+dragEl.getAttribute("url")+"'>"+
gtext(dragEl)+
"<\/a>";
if(rng.moveToPoint&&document.elementFromPoint){
tgt=document.elementFromPoint(evt.clientX,evt.clie ntY);
if(!hasAscendantNode(tgt,"a")&&
!hasAscendantNode(tgt,"input")&&
!hasAscendantNode(tgt,"textarea")){
rng.moveToPoint(evt.clientX,evt.clientY);
WRng(rng,s);
}
}else{
var c=evt.rangeParent,o=evt.rangeOffset;
if(c.nodeType==3){
c.splitText(o);
if(!hasAscendantNode(c,"a")&&
!hasAscendantNode(c,"input")&&
!hasAscendantNode(c,"textarea")){
rng.selectNode(c);
SRng(rng).collapse(false);
WRng(rng,s);
this.getSelection().removeAllRanges();
}
}
}
dragEl=null;
}else{
tgt=evt.originalTarget||evt.srcElement;
while(tgt&&tgt.nodeType!=1)tgt=tgt.parentNode;
if(tgt&&tgt.getAttribute("url"))
SRng(MRng(CRng(), dragEl=tgt));
}
};
})();
</script>

HTH
Yep.
Jul 23 '05 #3
Thanks a lot, YEP. This solution helps, but it doesn't solve some other
problems. Is there a problem to drag it with out selecting the element
<span>? At this point, I will only need to make it work with IE.

Another problem is, will the drag and drop be effective if it's between IE
and OUTLOOK? I tried something to extract information from span element, but
the result only show up in some non-html compliant editor. Outlook will
display the original stuff with html code hidden, which means if the <span
title="foo bar" styles="color:blue;">foo bar</span> is dragged into outlook,
the result will be a blue text with hidden information in it.

I cannot find any helpful information to get this working by googling. I was
suggested to disable the default mouse function on the object that's to be
dragged. but i have no idea how to do that... Sorry, i'm a newbie. :)

"Yann-Erwan Perio" <y-*******@em-lyon.com> wrote in message
news:40***********************@news.free.fr...
Wang, Jay wrote:
I would like to enable some text between <SPAN
url="http://www.testserver.com/">WORD TO BE DRAGGED </SPAN>. I put some
javascript and it will extract http://www.testserver.com/ from the the span element when I select the whole text in the SPAN and drag it.


This isn't an easy task, since you'd have to analyze text boundaries and
add/remove nodes in the tree. Ranges can help you, but they're supported
so far only by IE and Mozilla. You'll find an example below, not close
to satisfying, but which should nonetheless illustrate the use of many
range methods.

I've used an alternate click system instead of drag/drop, mostly because
of Mozilla not firing mouseup while dragging, and not offering ways to
prevent the drag (AFAIK).

I've also bumped into a strange bug as of Mozilla 1.7a, which would
require try/catch construct to be dealt with (adding an <input
type="text"> in the script, then clicking on it, fails miserably -
impossible to simply read a single property on the object).

<style type="text/css">
span[url]{background:yellow;}
</style>

<div>
Hello, <span url="foo.html">foo!</span>
You can drag and drop the foo where you want!
</div>

<script type="text/javascript">
window.onload=function(evt){
var d=document,rng;
if(d.body&&d.body.getAttribute&&d.body.nodeType&&
typeof d.body.parentNode!="undefined") {

if(d.body.createTextRange) rng=d.body.createTextRange();
else if(d.createRange) rng=d.createRange();
if(rng){
if(rng.moveToElementText&&rng.pasteHTML&&rng.moveT oPoint ||
rng.selectNode&&rng.insertNode&&rng.createContextu alFragment)
d.onclick=new Function("e","drag(e)");
}

}
}
var drag=(function(){

var dragEl;

function CRng(){
var d=document,rng;
if(d.body.createTextRange) { rng=d.body.createTextRange(); }
else if(d.createRange) { rng=d.createRange(); }
return rng;
}

function MRng(rng, el){
if(rng && el){
if(rng.moveToElementText) { rng.moveToElementText(el); }
else if(rng.selectNode) { rng.selectNode(el); }
}
return rng;
}

function SRng(rng){
if(rng){
if(rng.select) rng.select();
else{
with(this.getSelection()){
removeAllRanges();
addRange(rng);
}
}
}
return rng;
}

function WRng(rng,s){
if(rng){
if(rng.pasteHTML)rng.pasteHTML(s);
else rng.insertNode(rng.createContextualFragment(s));
}
return rng;
}

function hasAscendantNode(node,nodeName){
nodeName=nodeName.toLowerCase();
while(node&&node.nodeName.toLowerCase()!=nodeName)
node=node.parentNode;
return !!node;
}

function gtext(el){
if(typeof el.innerText!="undefined") return el.innerText;
else return (function(elmt){
for(var ii=0,buf=[],c=elmt.childNodes;ii<c.length;ii++){
if(c[ii].nodeType==3)buf.push(c[ii].nodeValue);
else if(c[ii].nodeType==1)buf.push(arguments.callee(c[ii]));
}
return buf.join("");
})(el);
}

return function(evt){
var tgt,rng,s,el;
evt=evt||window.event;

if(dragEl){
rng=CRng();
s="<a href='"+dragEl.getAttribute("url")+"'>"+
gtext(dragEl)+
"<\/a>";
if(rng.moveToPoint&&document.elementFromPoint){
tgt=document.elementFromPoint(evt.clientX,evt.clie ntY);
if(!hasAscendantNode(tgt,"a")&&
!hasAscendantNode(tgt,"input")&&
!hasAscendantNode(tgt,"textarea")){
rng.moveToPoint(evt.clientX,evt.clientY);
WRng(rng,s);
}
}else{
var c=evt.rangeParent,o=evt.rangeOffset;
if(c.nodeType==3){
c.splitText(o);
if(!hasAscendantNode(c,"a")&&
!hasAscendantNode(c,"input")&&
!hasAscendantNode(c,"textarea")){
rng.selectNode(c);
SRng(rng).collapse(false);
WRng(rng,s);
this.getSelection().removeAllRanges();
}
}
}
dragEl=null;
}else{
tgt=evt.originalTarget||evt.srcElement;
while(tgt&&tgt.nodeType!=1)tgt=tgt.parentNode;
if(tgt&&tgt.getAttribute("url"))
SRng(MRng(CRng(), dragEl=tgt));
}
};
})();
</script>

HTH
Yep.

Jul 23 '05 #4
Wang, Jay wrote:

Hello,
Is there a problem to drag it with out selecting the element
<span>?
At this point, I will only need to make it work with IE.
If you're using only IE, then you should have a look at the
clipboardData and dataTransfer objects, which permit to customize the
drag and drop.

<URL:http://msdn.microsoft.com/workshop/author/dhtml/reference/objects/obj_datatransfer.asp>

AIUI, these objects can only be used in IE, in other hosts you'd have to
check the relevant object model.
I tried something to extract information from span element, but
the result only show up in some non-html compliant editor.
A non-HTML compliant editor? Hmmm, that sounds familiar...Would you be
using FP or DW;-)
Outlook will
display the original stuff with html code hidden, which means if the <span
title="foo bar" styles="color:blue;">foo bar</span> is dragged into outlook,
the result will be a blue text with hidden information in it.


I can perfectly imagine that, though I cannot reproduce the problem
here. I'm unsure of what to do, to me that's a system setting, probably
in Outlook - check the relevant archives at http://groups.google.com/ or
wait for the knowledgeable to step by:-)

Unless I've completely misunderstood what you want to do, I don't think
you're in the right group - check the microsoft.public.* hierarchies for
Outlook newsgroups.
Regards,
Yep.
Jul 23 '05 #5
Thank you, YEP, here is the source code of the HTML file. If i can enable
the drag and drop on the span element without selecting it and then when the
span element is dropped, only the "title" information is dropped on the
target OUTLOOK. Does this make sense this time? I'm sorry if I didn't ask
the question clearly. Hope the source code will be helpful to get us on the
right track.

Jay

------------------------------------------------

<html xmlns:sp>
<head>
</head>
<script language="javascript">

/* ----------------------------------------------

Used to query for drag and drop

-------------------------------------------------*/

function initDropInfo(span)
{
var objEl = window.event.srcElement;
var src = objEl.src
var str= objEl.getAttribute("title");

window.event.dataTransfer.setData("TEXT",str);
window.event.dataTransfer.effectAllowed = "copyLink";
window.event.dataTransfer.dropEffect = 'move';
}

</script>
<body>
<span id="test" ondragstart="initDropInfo(this)"
title="http://www.test.com/test1.html" style='cursor:hand; color:blue;'
test drag and drop link</span> to extract "title" information in span element.

</body>
</html>

------------------------------------------------

"Yann-Erwan Perio" <y-*******@em-lyon.com> wrote in message
news:40***********************@news.free.fr... Wang, Jay wrote:

Hello,
Is there a problem to drag it with out selecting the element
<span>?
At this point, I will only need to make it work with IE.
If you're using only IE, then you should have a look at the
clipboardData and dataTransfer objects, which permit to customize the
drag and drop.

<URL:http://msdn.microsoft.com/workshop/a.../objects/obj_d
atatransfer.asp>
AIUI, these objects can only be used in IE, in other hosts you'd have to
check the relevant object model.
I tried something to extract information from span element, but
the result only show up in some non-html compliant editor.


A non-HTML compliant editor? Hmmm, that sounds familiar...Would you be
using FP or DW;-)
Outlook will
display the original stuff with html code hidden, which means if the <span title="foo bar" styles="color:blue;">foo bar</span> is dragged into outlook, the result will be a blue text with hidden information in it.


I can perfectly imagine that, though I cannot reproduce the problem
here. I'm unsure of what to do, to me that's a system setting, probably
in Outlook - check the relevant archives at http://groups.google.com/ or
wait for the knowledgeable to step by:-)

Unless I've completely misunderstood what you want to do, I don't think
you're in the right group - check the microsoft.public.* hierarchies for
Outlook newsgroups.
Regards,
Yep.

Jul 23 '05 #6
Wang, Jay wrote:
If i can enable
the drag and drop on the span element without selecting it and then when the
span element is dropped, only the "title" information is dropped on the
target OUTLOOK. Does this make sense this time?


Much more sense indeed:-)

The following will do, you were very close. Note that the link
capability only works when the message format is 'text' (Outlook
recognizes the dropped string to be a string, and puts a link around it)
- the way the string is treated should indeed depend on the receiving
host, and I don't think you can do much about this.

This is IE5.5+ only.
<script type="text/jscript">
function mdown(span){
var rng;
if(document.body && document.body.createTextRange){
rng=document.body.createTextRange();
rng.moveToElementText(span);
rng.select();
}
}

function mdrag(span){
var url=span.title;
if(window.event && window.event.dataTransfer){
with(window.event.dataTransfer){
setData("text", url);
effectAllowed="copy";
dropEffect="move";
}
}
}
</script>

<div>
This is a
<span
id="test"
onmousedown="mdown(this)"
ondragstart="mdrag(this)"
title="http://www.test.com/test1.html"
style="cursor:hand; color:blue;">test drag and drop link</span>
to extract "title" information in span element.
</div>
HTH
Yep.
Jul 23 '05 #7
This is awesome, YEP, you are the man!! :)
I saw the script still select the text between <SPAN> and then enable the
dragdrop action. This is very nice.

I'm wondering if we can control the default behavior of the mouse from
selecting text to none. basically, if i have a link html looks like: <a
href="http://test.com/test.html">test link should be dragged and dropped as
a whole entity</a>
The link will be dragged as an entity without having to selecting it. Are
there some tricks to enable this behavior on <span>?

Outlook display the whole <span> section and discard the javascipt action
definitely has something to do with the text being selected in the first
place.

Thanks again, Yann, You are extremely helpful.

Jay

"Yann-Erwan Perio" <y-*******@em-lyon.com> wrote in message
news:40***********************@news.free.fr...
Wang, Jay wrote:
If i can enable
the drag and drop on the span element without selecting it and then when the span element is dropped, only the "title" information is dropped on the
target OUTLOOK. Does this make sense this time?


Much more sense indeed:-)

The following will do, you were very close. Note that the link
capability only works when the message format is 'text' (Outlook
recognizes the dropped string to be a string, and puts a link around it)
- the way the string is treated should indeed depend on the receiving
host, and I don't think you can do much about this.

This is IE5.5+ only.
<script type="text/jscript">
function mdown(span){
var rng;
if(document.body && document.body.createTextRange){
rng=document.body.createTextRange();
rng.moveToElementText(span);
rng.select();
}
}

function mdrag(span){
var url=span.title;
if(window.event && window.event.dataTransfer){
with(window.event.dataTransfer){
setData("text", url);
effectAllowed="copy";
dropEffect="move";
}
}
}
</script>

<div>
This is a
<span
id="test"
onmousedown="mdown(this)"
ondragstart="mdrag(this)"
title="http://www.test.com/test1.html"
style="cursor:hand; color:blue;">test drag and drop link</span>
to extract "title" information in span element.
</div>
HTH
Yep.

Jul 23 '05 #8
Wang, Jay wrote:
I'm wondering if we can control the default behavior of the mouse from
selecting text to none.
We can, but the drag and drop needs some text to be selected in order to
be triggered, so that wouldn't work (you can however clear the selection
*after* the drag has started, see below).
The link will be dragged as an entity without having to selecting it. Are
there some tricks to enable this behavior on <span>?
I don't think so, moreover in my computer the link is dragged as an
attachment in Outlook:-) Of course you could create a link before
starting the drag, put the SPAN inside and remove it afterwards, but...
why not use a link straightforward if the behavior it exposes just
happens to be what you're looking for?
Outlook display the whole <span> section and discard the javascipt action
definitely has something to do with the text being selected in the first
place.
An interesting hypothesis, which turns out to be true to a certain
extent. Just use:

function mdrag(span){
var url=span.title;
document.selection.empty();
if(window.event && window.event.dataTransfer){
with(window.event.dataTransfer){
effectAllowed="copyLink";
dropEffect="move";
setData("text", url);
}
}
}

....instead of the previous mdrag function and see if that works the way
you want.
Thanks again, Yann, You are extremely helpful.


Well I'm glad if that helps, but I'm still missing the big picture! If
you could provide more insight on the context then we might suggest
other approaches, perhaps more adapted to the situation (unless you're
dealing with a very specific requirement).
Regards,
Yep.
Jul 23 '05 #9
Thanks a lot, YEP. You understand me so well. :)
"document.selection.empty();" solved every problem. Outlook is taking the
extracted properties perfectly right now.
I appreciate your help so much. Thanks a million! :)

Jay

"Yann-Erwan Perio" <y-*******@em-lyon.com> wrote in message
news:40***********************@news.free.fr...
Wang, Jay wrote:
I'm wondering if we can control the default behavior of the mouse from
selecting text to none.


We can, but the drag and drop needs some text to be selected in order to
be triggered, so that wouldn't work (you can however clear the selection
*after* the drag has started, see below).
The link will be dragged as an entity without having to selecting it. Are there some tricks to enable this behavior on <span>?


I don't think so, moreover in my computer the link is dragged as an
attachment in Outlook:-) Of course you could create a link before
starting the drag, put the SPAN inside and remove it afterwards, but...
why not use a link straightforward if the behavior it exposes just
happens to be what you're looking for?
Outlook display the whole <span> section and discard the javascipt action definitely has something to do with the text being selected in the first
place.


An interesting hypothesis, which turns out to be true to a certain
extent. Just use:

function mdrag(span){
var url=span.title;
document.selection.empty();
if(window.event && window.event.dataTransfer){
with(window.event.dataTransfer){
effectAllowed="copyLink";
dropEffect="move";
setData("text", url);
}
}
}

...instead of the previous mdrag function and see if that works the way
you want.
Thanks again, Yann, You are extremely helpful.


Well I'm glad if that helps, but I'm still missing the big picture! If
you could provide more insight on the context then we might suggest
other approaches, perhaps more adapted to the situation (unless you're
dealing with a very specific requirement).
Regards,
Yep.

Jul 23 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Alexander Ross | last post by:
I have an html snippet, and I want to remove any <span> tags that have a specific attribute (class=none) ex. The quick brown <span class="animal">fox</span> jumped <span...
23
by: Mikko Ohtamaa | last post by:
From XML specification: The representation of an empty element is either a start-tag immediately followed by an end-tag, or an empty-element tag. (This means that <foo></foo> is equal to...
4
by: DMJ | last post by:
What is the difference between these two tags?
3
by: Andrew Tang | last post by:
Hi. I'm having problems with the following code. The last line seems to be in the wrong place, its been moved down slightly by about 3 pixels in IE6. <html> <head> <title>Lorem Ipsum</title>...
18
by: Timothy Casey | last post by:
Thanks in advance... =~= Timothy Casey South Australia worloq@iprimus.com.au Formerly: casey@smart.net.au
2
by: Mervyn Carter | last post by:
Dear Friends IE5+, Win9*, not Netscape - I understand getSelection() is Netscape only. Anyone know of a way to access an area of mouse selected text within a <SPAN> or <DIV> ( ie not in a...
2
by: Don Wash | last post by:
Hi There! I'm creating my website with ASP.NET + XHTML, which means I will strictly adhere the XHTML standards for my web page output. I use Panel or PlaceHolder WebControls to place...
1
by: shivani anand | last post by:
Hi.. I have a doubt. I have some code.. <span class="test1"> my screeen </span> I simply changed it to. <h2 class = "test1"> my screen </h2>
1
by: rynato | last post by:
I have a <spanof width X px and height Y px. I want to read the text of an article, which is stored in a mySQL table, and pass to that <spanonly just enough text to fit in it, along with a 'read...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.