yuvarajr
asked on
highlight selected text on html page
Hi Experts,
Scenario: The user selects or blocks any line or sentence on a html page using mouse. I need to provide a highlight button which allows the user to highlight the selected text on the html page with any color(which is very similar to highlight function in microsoft word). Also i do not want to change the background color of the selected html text to achieve this. I thought of doing this using javascript layers but if the user blocks the last word in one line and the first word in the next line i don't know how to create a layer for that. Is it possible to do that using layers or by any other means??..Pls help..
Scenario: The user selects or blocks any line or sentence on a html page using mouse. I need to provide a highlight button which allows the user to highlight the selected text on the html page with any color(which is very similar to highlight function in microsoft word). Also i do not want to change the background color of the selected html text to achieve this. I thought of doing this using javascript layers but if the user blocks the last word in one line and the first word in the next line i don't know how to create a layer for that. Is it possible to do that using layers or by any other means??..Pls help..
Is this what you mean?
<script>
var clr='red';
function hili(){
document.execCommand("Fore color",0,c lr);
}
</script>
Here is some text...la la la, blah blah blah....<br>
More text more text...
<p><button onclick=hili();>Hilight Text</button>
<button onclick="clr='green';">Cha nge Colour to Green</button>
<script>
var clr='red';
function hili(){
document.execCommand("Fore
}
</script>
Here is some text...la la la, blah blah blah....<br>
More text more text...
<p><button onclick=hili();>Hilight Text</button>
<button onclick="clr='green';">Cha
Sorry, I think I misunderstood your question.
Why do you not want to change the background of the text?
Why do you not want to change the background of the text?
ASKER
Hi Guys,
Thx a lot for ur suggestions. Here is what i want to do:
I want to have 2 buttons
1.for highlighting any part of html
2.for creating a bookmark. I want to place a bookmark icon on any place on html and add some comments - i can do that by opening a new window and allow user to type text. Also i want to allow the user to move the bookmark icon on any place on the html. So to do this i was thinking of using javascript layers. Is this the right choice or is there an easy way to do this?.
Is it possible to use layers for highlight as well?. I wanted to do both highlight and bookmark without modifying the underlying html. So i could not find anything other than layers.
Also i need to store the location of bookmark and highlight and their texts in a database table so that i can display a list of both to the user. I can do the listing using asp.net datagrid.
But which is the best create both highlight and bookmark and to how store their location?. Should i store the x and y co-ordinates of them in html?
Also how to navigate to that particular position on html once the user selects a particular item in the list?.
Thanks....
Thx a lot for ur suggestions. Here is what i want to do:
I want to have 2 buttons
1.for highlighting any part of html
2.for creating a bookmark. I want to place a bookmark icon on any place on html and add some comments - i can do that by opening a new window and allow user to type text. Also i want to allow the user to move the bookmark icon on any place on the html. So to do this i was thinking of using javascript layers. Is this the right choice or is there an easy way to do this?.
Is it possible to use layers for highlight as well?. I wanted to do both highlight and bookmark without modifying the underlying html. So i could not find anything other than layers.
Also i need to store the location of bookmark and highlight and their texts in a database table so that i can display a list of both to the user. I can do the listing using asp.net datagrid.
But which is the best create both highlight and bookmark and to how store their location?. Should i store the x and y co-ordinates of them in html?
Also how to navigate to that particular position on html once the user selects a particular item in the list?.
Thanks....
Why can't you simple change the background colour? Using layers is possible, but much more complicated. Here's a script I put together:
-------------------------- ---------- ---------- --------
<script>
var clr='yellow';
var offset=2,lineHgt;
function hili(){
var sel=document.selection.cre ateRange() ;
if(sel.boundingHeight>line Hgt){
createHili(sel.boundingTop -offset,se l.offsetLe ft-offset, sel.boundi ngWidth-se l.offsetLe ft+sel.bou ndingLeft, lineHgt,cl r);
for(i=1;i<parseInt(sel.bou ndingHeigh t/lineHgt) -1;i++){
createHili(sel.boundingTop -offset+li neHgt*i,se l.bounding Left-offse t,sel.boun dingWidth, lineHgt,cl r);
}
var newsel=sel.duplicate();
while(sel.inRange(newsel)) {
newsel.moveStart("characte r");
}newsel.moveStart("charact er",-1);
createHili(newsel.bounding Top-offset ,sel.bound ingLeft-of fset,newse l.bounding Left-sel.b oundingLef t,lineHgt, clr);
}else{
createHili(sel.boundingTop -offset,se l.bounding Left-offse t,sel.boun dingWidth, sel.boundi ngHeight,c lr);
}
}
function createHili(t,l,w,h,clr){
//alert(t+','+l+','+w+','+ h)
var div=document.createElement ("div");
div.style.height=h;div.sty le.width=w ;
div.style.left=l;div.style .top=t;
div.style.position="absolu te";div.st yle.fontSi ze="0px";d iv.style.b ackgroundC olor=clr;d iv.innerHT ML=" " ;div.style .zIndex=-1 ;
document.body.appendChild( div);
}
function init(){
var div=document.createElement ("div");
div.innerHTML="1";
document.body.appendChild( div);
lineHgt=div.offsetHeight;
div.parentNode.removeChild (div);
}
</script>
<body onload="init();">
<button onclick="hili();">Hilight Text</button>
<div id=content>
Here is some text... Here is some text... Here is some text... Here is some text... Here is some text...
Here is some text... Here is some text... Here is some text... Here is some text... Here is some text...
Here is some text... Here is some text... Here is some text... Here is some text... Here is some text...
Here is some text... Here is some text... Here is some text... Here is some text... Here is some text...
Here is some text... Here is some text... Here is some text... Here is some text... Here is some text...
Here is some text... Here is some text... Here is some text... Here is some text... Here is some text...
</div>
--------------------------
<script>
var clr='yellow';
var offset=2,lineHgt;
function hili(){
var sel=document.selection.cre
if(sel.boundingHeight>line
createHili(sel.boundingTop
for(i=1;i<parseInt(sel.bou
createHili(sel.boundingTop
}
var newsel=sel.duplicate();
while(sel.inRange(newsel))
newsel.moveStart("characte
}newsel.moveStart("charact
createHili(newsel.bounding
}else{
createHili(sel.boundingTop
}
}
function createHili(t,l,w,h,clr){
//alert(t+','+l+','+w+','+
var div=document.createElement
div.style.height=h;div.sty
div.style.left=l;div.style
div.style.position="absolu
document.body.appendChild(
}
function init(){
var div=document.createElement
div.innerHTML="1";
document.body.appendChild(
lineHgt=div.offsetHeight;
div.parentNode.removeChild
}
</script>
<body onload="init();">
<button onclick="hili();">Hilight Text</button>
<div id=content>
Here is some text... Here is some text... Here is some text... Here is some text... Here is some text...
Here is some text... Here is some text... Here is some text... Here is some text... Here is some text...
Here is some text... Here is some text... Here is some text... Here is some text... Here is some text...
Here is some text... Here is some text... Here is some text... Here is some text... Here is some text...
Here is some text... Here is some text... Here is some text... Here is some text... Here is some text...
Here is some text... Here is some text... Here is some text... Here is some text... Here is some text...
</div>
If you don't want to change the underlying html, can't you just store the html in a variable beforehand?
I don't completely understand your bookmark question. Please explain in more detail. Furthermore, that wasn't in your original question.
I don't completely understand your bookmark question. Please explain in more detail. Furthermore, that wasn't in your original question.
I also don't understand what you mean by storing the location, do you mean the top left coordinates of the block of text? If so, this will do the trick:
<script>
var clr='yellow';
var offset=2,lineHgt;
var hiliText=new Array();
var hiliLoc=new Array();
var hiliDiv=new Array();
function hili(){
var sel=document.selection.cre ateRange() ;
if(sel.boundingHeight>line Hgt){
createHili(sel.boundingTop -offset,se l.offsetLe ft-offset, sel.boundi ngWidth-se l.offsetLe ft+sel.bou ndingLeft, lineHgt,cl r);
for(i=1;i<parseInt(sel.bou ndingHeigh t/lineHgt) -1;i++){
createHili(sel.boundingTop -offset+li neHgt*i,se l.bounding Left-offse t,sel.boun dingWidth, lineHgt,cl r);
}
var newsel=sel.duplicate();
while(sel.inRange(newsel)) {
newsel.moveStart("characte r");
}newsel.moveStart("charact er",-1);
createHili(newsel.bounding Top-offset ,sel.bound ingLeft-of fset,newse l.bounding Left-sel.b oundingLef t,lineHgt, clr);
}else{
createHili(sel.boundingTop -offset,se l.bounding Left-offse t,sel.boun dingWidth, sel.boundi ngHeight,c lr);
}
hiliText[hiliText.length]= sel.text;
hiliLoc[hiliLoc.length]=(s el.offsetL eft-offset )+","+(sel .offsetTop -offset);
}
function createHili(t,l,w,h,clr){
var div=document.createElement ("div");
div.style.height=h;div.sty le.width=w ;
div.style.left=l;div.style .top=t;
div.style.position="absolu te";div.st yle.fontSi ze="0px";d iv.style.b ackgroundC olor=clr;d iv.innerHT ML=" " ;div.style .zIndex=-1 ;
document.body.appendChild( div);
hiliDiv[hiliDiv.length]=di v;
}
function showHili(){
alert("Text:\n\n---------- ---------- -------\n" +hiliText. join('\n-- ---------- ---------- -----\n')) ;
alert("Positions:\n\n----- ---------- ---------- --\n"+hili Loc.join(' \n-------- ---------- ---------\ n'));
}
function clearHili(){
for(i=0;i<hiliDiv.length;i ++){
hiliDiv[i].parentNode.remo veChild(hi liDiv[i]);
}
hiliText.length=0;hiliLoc. length=0;
}
function init(){
var div=document.createElement ("div");
div.innerHTML="1";
document.body.appendChild( div);
lineHgt=div.offsetHeight;
div.parentNode.removeChild (div);
}
</script>
<body onload="init();">
<button onclick="hili();">Hilight Text</button>
<button onclick="showHili();">Show list of hilighted text</button>
<button onclick="clearHili();">Cle ar All</button>
<div id=content>
Here is some text... Here is some text... Here is some text... Here is some text... Here is some text...
Here is some text... Here is some text... Here is some text... Here is some text... Here is some text...
Here is some text... Here is some text... Here is some text... Here is some text... Here is some text...
Here is some text... Here is some text... Here is some text... Here is some text... Here is some text...
Here is some text... Here is some text... Here is some text... Here is some text... Here is some text...
Here is some text... Here is some text... Here is some text... Here is some text... Here is some text...
</div>
<script>
var clr='yellow';
var offset=2,lineHgt;
var hiliText=new Array();
var hiliLoc=new Array();
var hiliDiv=new Array();
function hili(){
var sel=document.selection.cre
if(sel.boundingHeight>line
createHili(sel.boundingTop
for(i=1;i<parseInt(sel.bou
createHili(sel.boundingTop
}
var newsel=sel.duplicate();
while(sel.inRange(newsel))
newsel.moveStart("characte
}newsel.moveStart("charact
createHili(newsel.bounding
}else{
createHili(sel.boundingTop
}
hiliText[hiliText.length]=
hiliLoc[hiliLoc.length]=(s
}
function createHili(t,l,w,h,clr){
var div=document.createElement
div.style.height=h;div.sty
div.style.left=l;div.style
div.style.position="absolu
document.body.appendChild(
hiliDiv[hiliDiv.length]=di
}
function showHili(){
alert("Text:\n\n----------
alert("Positions:\n\n-----
}
function clearHili(){
for(i=0;i<hiliDiv.length;i
hiliDiv[i].parentNode.remo
}
hiliText.length=0;hiliLoc.
}
function init(){
var div=document.createElement
div.innerHTML="1";
document.body.appendChild(
lineHgt=div.offsetHeight;
div.parentNode.removeChild
}
</script>
<body onload="init();">
<button onclick="hili();">Hilight Text</button>
<button onclick="showHili();">Show
<button onclick="clearHili();">Cle
<div id=content>
Here is some text... Here is some text... Here is some text... Here is some text... Here is some text...
Here is some text... Here is some text... Here is some text... Here is some text... Here is some text...
Here is some text... Here is some text... Here is some text... Here is some text... Here is some text...
Here is some text... Here is some text... Here is some text... Here is some text... Here is some text...
Here is some text... Here is some text... Here is some text... Here is some text... Here is some text...
Here is some text... Here is some text... Here is some text... Here is some text... Here is some text...
</div>
ASKER
Hi lil_puffball,
Thx a lot for ur help..This code is wonderful..
I wanted to store the x & y co-ordinates of the highlighted text, b'cos if that if the user comes back to the same html again i've to show the previously highlighted text as highlighted. I hope i'm clear now...
So when the user comes back to the page again, i guess i can create highlight by passing the co-ordinates to the hili() function or by sighlightly modifying the hili() function..is that right?. And if the html body is not inside div tag then i need to add div tag to the html body before displaying to the user..is that right?
I'm sorry i forgot to include the bookmark part when i posted first. I dont mind giving you additional points if you could help me to crack down this. Here is what i want to do:
I've a button called Addbookmark. Once the user clicks on Addbookmark button and clicks anywhere inside html i need to open a new window and allow him to type in some comments. And once he closes the new window i need to place a bookmark icon at the place where he clicked last on the html. Also i need to allow the user to move the bookmark icon anywhere on the html. (This is the reason why i brought up using javascript layers). Similar to highlight i need to store the co-ordinates of this bookmark icon as well in a database, so that when the user comes to this page again i can show the bookmark and allow the user to click and view the comments. Am i making sense now??
Thx in advance...
Thx a lot for ur help..This code is wonderful..
I wanted to store the x & y co-ordinates of the highlighted text, b'cos if that if the user comes back to the same html again i've to show the previously highlighted text as highlighted. I hope i'm clear now...
So when the user comes back to the page again, i guess i can create highlight by passing the co-ordinates to the hili() function or by sighlightly modifying the hili() function..is that right?. And if the html body is not inside div tag then i need to add div tag to the html body before displaying to the user..is that right?
I'm sorry i forgot to include the bookmark part when i posted first. I dont mind giving you additional points if you could help me to crack down this. Here is what i want to do:
I've a button called Addbookmark. Once the user clicks on Addbookmark button and clicks anywhere inside html i need to open a new window and allow him to type in some comments. And once he closes the new window i need to place a bookmark icon at the place where he clicked last on the html. Also i need to allow the user to move the bookmark icon anywhere on the html. (This is the reason why i brought up using javascript layers). Similar to highlight i need to store the co-ordinates of this bookmark icon as well in a database, so that when the user comes to this page again i can show the bookmark and allow the user to click and view the comments. Am i making sense now??
Thx in advance...
Extra points are not necessary. Just remember to include your full requirements in the question. :)
Sorry about the delay, the bookmark thing was more difficult than I expected. I'm not sure how you want to insert the bookmark, please take a look at this example and let me know if this is what you want. I will also work on the position part.
-------------------------- ---------- ---------- ---------- ---
<script>
var clr='yellow';
var offset=2,lineHgt;
var hiliText=new Array();
var hiliLoc=new Array();
var hiliDiv=new Array();
var x,y,cursor;
function hili(){
var sel=document.selection.cre ateRange() ;
if(sel.boundingHeight>line Hgt){
createHili(sel.boundingTop -offset,se l.offsetLe ft-offset, sel.boundi ngWidth-se l.offsetLe ft+sel.bou ndingLeft, lineHgt,cl r);
for(i=1;i<parseInt(sel.bou ndingHeigh t/lineHgt) -1;i++){
createHili(sel.boundingTop -offset+li neHgt*i,se l.bounding Left-offse t,sel.boun dingWidth, lineHgt,cl r);
}
var newsel=sel.duplicate();
while(sel.inRange(newsel)) {
newsel.moveStart("characte r");
}newsel.moveStart("charact er",-1);
createHili(newsel.bounding Top-offset ,sel.bound ingLeft-of fset,newse l.bounding Left-sel.b oundingLef t,lineHgt, clr);
}else{
createHili(sel.boundingTop -offset,se l.bounding Left-offse t,sel.boun dingWidth, sel.boundi ngHeight,c lr);
}
hiliText[hiliText.length]= sel.text;
hiliLoc[hiliLoc.length]=(s el.offsetL eft-offset )+","+(sel .offsetTop -offset);
}
function createHili(t,l,w,h,clr){
var div=document.createElement ("div");
div.style.height=h;div.sty le.width=w ;
div.style.left=l;div.style .top=t;
div.style.position="absolu te";div.st yle.fontSi ze="0px";d iv.style.b ackgroundC olor=clr;d iv.innerHT ML=" " ;div.style .zIndex=-1 ;
document.body.appendChild( div);
hiliDiv[hiliDiv.length]=di v;
}
function showHili(){
alert("Text:\n\n---------- ---------- -------\n" +hiliText. join('\n-- ---------- ---------- -----\n')) ;
alert("Positions:\n\n----- ---------- ---------- --\n"+hili Loc.join(' \n-------- ---------- ---------\ n'));
}
function clearHili(){
for(i=0;i<hiliDiv.length;i ++){
hiliDiv[i].parentNode.remo veChild(hi liDiv[i]);
}
hiliText.length=0;hiliLoc. length=0;
}
function insertBookmark(){
var text=prompt("Enter the text:","Bookmark 1");
cursor.pasteHTML("<img src=bookmark.gif height=15 title='"+text+"'>");
}
function init(){
var div=document.createElement ("div");
div.innerHTML="1";
document.body.appendChild( div);
lineHgt=div.offsetHeight;
div.parentNode.removeChild (div);
cursor=getDivText();cursor .collapse( );
}
function setCursor(){
cursor=getDivText();
var end=cursor.duplicate();
end.collapse(false);
cursor.collapse();
var pos=cursor.duplicate(),cnt =0;
//find row
while(y-pos.boundingTop+of fset>0&&!p os.inRange (end)){//p revDif=y-p os.boundin gTop;pos.m ove("chara cter");cnt ++;}
var prev=pos.boundingTop;
while(pos.boundingTop==pre v&&!pos.in Range(end) ){pos.move ("characte r");cnt++; }
}
//move back 1
pos.move("character",-1);c nt--;
//find column
while(pos.boundingLeft-off set>x){
pos.move("character",-1);c nt--;
}
//change cursor
cursor.moveStart("characte r",cnt);
}
function getDivText(){
var sel=document.body.createTe xtRange();
// sel.moveEnd("character",-3 );
while(sel.parentElement()! =document. getElement ById("cont ent")){sel .moveStart ("characte r");}
return sel;
}
</script>
<body onload="init();" onmousemove="x=event.x;y=e vent.y;">
<button onclick="hili();">Hilight Text</button>
<button onclick="showHili();">Show list of hilighted text</button>
<button onclick="clearHili();">Cle ar All</button>
<button onclick="insertBookmark(); ">Insert Bookmark</button>
<div id=content onclick="setCursor();">
Here is some text... Here is some text... Here is some text... Here is some text... Here is some text...
Here is some text... Here is some text... Here is some text... Here is some text... Here is some text...
Here is some text... Here is some text... Here is some text... Here is some text... Here is some text...
Here is some text... Here is some text... Here is some text... Here is some text... Here is some text...
Here is some text... Here is some text... Here is some text... Here is some text... Here is some text...
Here is some text... Here is some text... Here is some text... Here is some text... Here is some text...
</div>
Sorry about the delay, the bookmark thing was more difficult than I expected. I'm not sure how you want to insert the bookmark, please take a look at this example and let me know if this is what you want. I will also work on the position part.
--------------------------
<script>
var clr='yellow';
var offset=2,lineHgt;
var hiliText=new Array();
var hiliLoc=new Array();
var hiliDiv=new Array();
var x,y,cursor;
function hili(){
var sel=document.selection.cre
if(sel.boundingHeight>line
createHili(sel.boundingTop
for(i=1;i<parseInt(sel.bou
createHili(sel.boundingTop
}
var newsel=sel.duplicate();
while(sel.inRange(newsel))
newsel.moveStart("characte
}newsel.moveStart("charact
createHili(newsel.bounding
}else{
createHili(sel.boundingTop
}
hiliText[hiliText.length]=
hiliLoc[hiliLoc.length]=(s
}
function createHili(t,l,w,h,clr){
var div=document.createElement
div.style.height=h;div.sty
div.style.left=l;div.style
div.style.position="absolu
document.body.appendChild(
hiliDiv[hiliDiv.length]=di
}
function showHili(){
alert("Text:\n\n----------
alert("Positions:\n\n-----
}
function clearHili(){
for(i=0;i<hiliDiv.length;i
hiliDiv[i].parentNode.remo
}
hiliText.length=0;hiliLoc.
}
function insertBookmark(){
var text=prompt("Enter the text:","Bookmark 1");
cursor.pasteHTML("<img src=bookmark.gif height=15 title='"+text+"'>");
}
function init(){
var div=document.createElement
div.innerHTML="1";
document.body.appendChild(
lineHgt=div.offsetHeight;
div.parentNode.removeChild
cursor=getDivText();cursor
}
function setCursor(){
cursor=getDivText();
var end=cursor.duplicate();
end.collapse(false);
cursor.collapse();
var pos=cursor.duplicate(),cnt
//find row
while(y-pos.boundingTop+of
var prev=pos.boundingTop;
while(pos.boundingTop==pre
}
//move back 1
pos.move("character",-1);c
//find column
while(pos.boundingLeft-off
pos.move("character",-1);c
}
//change cursor
cursor.moveStart("characte
}
function getDivText(){
var sel=document.body.createTe
// sel.moveEnd("character",-3
while(sel.parentElement()!
return sel;
}
</script>
<body onload="init();" onmousemove="x=event.x;y=e
<button onclick="hili();">Hilight Text</button>
<button onclick="showHili();">Show
<button onclick="clearHili();">Cle
<button onclick="insertBookmark();
<div id=content onclick="setCursor();">
Here is some text... Here is some text... Here is some text... Here is some text... Here is some text...
Here is some text... Here is some text... Here is some text... Here is some text... Here is some text...
Here is some text... Here is some text... Here is some text... Here is some text... Here is some text...
Here is some text... Here is some text... Here is some text... Here is some text... Here is some text...
Here is some text... Here is some text... Here is some text... Here is some text... Here is some text...
Here is some text... Here is some text... Here is some text... Here is some text... Here is some text...
</div>
ASKER
Hi lil_puffball,
Thx a lot for ur efforts..
Is it possible to place the bookmark icon over the text instead of placing it besides the text?..also my concern is that the user should be able to move the bookmark anywhere over the html screen...
And similar to highlight i need to store the position of bookmark on the screen and the bookmark comments in a database..
Here is something i found on a site which i thought might be useful to you:
http://www.dansteinman.com/dynapi/docs/examples/drag3.html
Thx..
Thx a lot for ur efforts..
Is it possible to place the bookmark icon over the text instead of placing it besides the text?..also my concern is that the user should be able to move the bookmark anywhere over the html screen...
And similar to highlight i need to store the position of bookmark on the screen and the bookmark comments in a database..
Here is something i found on a site which i thought might be useful to you:
http://www.dansteinman.com/dynapi/docs/examples/drag3.html
Thx..
yuvarajr,
Do you care if the bookmark must snap to the text, or just anywhere the mouse is?
The second way would be a lot easier...
Do you care if the bookmark must snap to the text, or just anywhere the mouse is?
The second way would be a lot easier...
Here is the new script. I will work on the location stuff soon.
-------------------------- ---------- ---------- --------
<!--//txt=txt.replace(/<p> .*<\/p>/gi ,function( p1){return p1.replace(/\r\n?/gi,"");} );-->
<script>
var clr='yellow';
var offset=2,lineHgt;
var hiliText=new Array();
var hiliLoc=new Array();
var hiliDiv=new Array();
var bkmkText=new Array();
var bkmkLoc=new Array();
var bkmkDiv=new Array();
var x,y,cursorX=null,cursorY=n ull;
function hili(){
var sel=document.selection.cre ateRange() ;
if(sel.boundingHeight>line Hgt){
createHili(sel.boundingTop -offset,se l.offsetLe ft-offset, sel.boundi ngWidth-se l.offsetLe ft+sel.bou ndingLeft, lineHgt,cl r);
for(i=1;i<parseInt(sel.bou ndingHeigh t/lineHgt) -1;i++){
createHili(sel.boundingTop -offset+li neHgt*i,se l.bounding Left-offse t,sel.boun dingWidth, lineHgt,cl r);
}
var newsel=sel.duplicate();
while(sel.inRange(newsel)) {
newsel.moveStart("characte r");
}newsel.moveStart("charact er",-1);
createHili(newsel.bounding Top-offset ,sel.bound ingLeft-of fset,newse l.bounding Left-sel.b oundingLef t,lineHgt, clr);
}else{
createHili(sel.boundingTop -offset,se l.bounding Left-offse t,sel.boun dingWidth, sel.boundi ngHeight,c lr);
}
hiliText[hiliText.length]= sel.text;
hiliLoc[hiliLoc.length]=(s el.offsetL eft-offset )+","+(sel .offsetTop -offset);
}
function createHili(t,l,w,h,clr){
var div=document.createElement ("div");
div.style.height=h;div.sty le.width=w ;
div.style.left=l;div.style .top=t;
div.style.position="absolu te";div.st yle.fontSi ze="0px";d iv.style.b ackgroundC olor=clr;d iv.innerHT ML=" " ;div.style .zIndex=-1 ;
document.body.appendChild( div);
hiliDiv[hiliDiv.length]=di v;
}
function clearHili(){
for(i=0;i<hiliDiv.length;i ++){
hiliDiv[i].parentNode.remo veChild(hi liDiv[i]);
}
hiliText.length=0;hiliLoc. length=0;h iliDiv.len gth=0;
}
function clearBookmarks(){
for(i=0;i<bkmkDiv.length;i ++){
bkmkDiv[i].parentNode.remo veChild(bk mkDiv[i]);
}
bkmkText.length=0;bkmkLoc. length=0;b kmkDiv.len gth=0;
}
function insertBookmark(){
if(!cursorX){alert("Please click somewhere first.");return;}
var text=prompt("Enter the text:","Bookmark "+(bkmkDiv.length+1));
var img=document.createElement ("div");
img.id="bookmark"+bkmkDiv. length;
img.style.position="absolu te";
img.style.top=cursorY;
img.style.left=cursorX;
img.innerHTML="<img src='bookmark.gif' title='"+text+"' height=15 unselectable=on>";
img.unselectable="on";
img.onmousedown=function() {startDrag (this);}
document.body.appendChild( img);
bkmkDiv[bkmkDiv.length]=im g;
bkmkText[bkmkText.length]= text;
bkmkLoc[bkmkLoc.length]=cu rsorX+","+ cursorY;
}
var offL,offT,drag=false,obj;
function startDrag(srcEl){obj=srcEl ;offL=x-ge tLeft(obj) ;offT=y-ge tTop(obj); drag=true; }
function dragObj(){obj.style.left=x -offL;obj. style.top= y-offT;}
function getLeft(obj){return parseInt(obj.style.left);}
function getTop(obj){return parseInt(obj.style.top);}
function init(){
var div=document.createElement ("div");
div.innerHTML="1";
document.body.appendChild( div);
lineHgt=div.offsetHeight;
div.parentNode.removeChild (div);
}
</script>
<body onload="init();" onmousemove="x=event.x;y=e vent.y;if( drag){drag Obj();}" onmouseup="if(drag){drag=f alse;bkmkL oc[obj.id. charAt(obj .id.length -1)]=x+',' +y;}">
<button onclick="hili();">Hilight Text</button>
<button onclick="clearHili();">Cle ar All Highlights</button>
<button onclick="insertBookmark(); ">Insert Bookmark</button>
<button onclick="clearBookmarks(); ">Clear All Bookmarks</button>
<div id=content onclick="cursorX=x;cursorY =y;">
Here is some text... Here is some text... Here is some text... Here is some text... Here is some text...
Here is some text... Here is some text... Here is some text... Here is some text... Here is some text...
Here is some text... Here is some text... Here is some text... Here is some text... Here is some text...
Here is some text... Here is some text... Here is some text... Here is some text... Here is some text...
Here is some text... Here is some text... Here is some text... Here is some text... Here is some text...
Here is some text... Here is some text... Here is some text... Here is some text... Here is some text...
</div>
--------------------------
<!--//txt=txt.replace(/<p>
<script>
var clr='yellow';
var offset=2,lineHgt;
var hiliText=new Array();
var hiliLoc=new Array();
var hiliDiv=new Array();
var bkmkText=new Array();
var bkmkLoc=new Array();
var bkmkDiv=new Array();
var x,y,cursorX=null,cursorY=n
function hili(){
var sel=document.selection.cre
if(sel.boundingHeight>line
createHili(sel.boundingTop
for(i=1;i<parseInt(sel.bou
createHili(sel.boundingTop
}
var newsel=sel.duplicate();
while(sel.inRange(newsel))
newsel.moveStart("characte
}newsel.moveStart("charact
createHili(newsel.bounding
}else{
createHili(sel.boundingTop
}
hiliText[hiliText.length]=
hiliLoc[hiliLoc.length]=(s
}
function createHili(t,l,w,h,clr){
var div=document.createElement
div.style.height=h;div.sty
div.style.left=l;div.style
div.style.position="absolu
document.body.appendChild(
hiliDiv[hiliDiv.length]=di
}
function clearHili(){
for(i=0;i<hiliDiv.length;i
hiliDiv[i].parentNode.remo
}
hiliText.length=0;hiliLoc.
}
function clearBookmarks(){
for(i=0;i<bkmkDiv.length;i
bkmkDiv[i].parentNode.remo
}
bkmkText.length=0;bkmkLoc.
}
function insertBookmark(){
if(!cursorX){alert("Please
var text=prompt("Enter the text:","Bookmark "+(bkmkDiv.length+1));
var img=document.createElement
img.id="bookmark"+bkmkDiv.
img.style.position="absolu
img.style.top=cursorY;
img.style.left=cursorX;
img.innerHTML="<img src='bookmark.gif' title='"+text+"' height=15 unselectable=on>";
img.unselectable="on";
img.onmousedown=function()
document.body.appendChild(
bkmkDiv[bkmkDiv.length]=im
bkmkText[bkmkText.length]=
bkmkLoc[bkmkLoc.length]=cu
}
var offL,offT,drag=false,obj;
function startDrag(srcEl){obj=srcEl
function dragObj(){obj.style.left=x
function getLeft(obj){return parseInt(obj.style.left);}
function getTop(obj){return parseInt(obj.style.top);}
function init(){
var div=document.createElement
div.innerHTML="1";
document.body.appendChild(
lineHgt=div.offsetHeight;
div.parentNode.removeChild
}
</script>
<body onload="init();" onmousemove="x=event.x;y=e
<button onclick="hili();">Hilight Text</button>
<button onclick="clearHili();">Cle
<button onclick="insertBookmark();
<button onclick="clearBookmarks();
<div id=content onclick="cursorX=x;cursorY
Here is some text... Here is some text... Here is some text... Here is some text... Here is some text...
Here is some text... Here is some text... Here is some text... Here is some text... Here is some text...
Here is some text... Here is some text... Here is some text... Here is some text... Here is some text...
Here is some text... Here is some text... Here is some text... Here is some text... Here is some text...
Here is some text... Here is some text... Here is some text... Here is some text... Here is some text...
Here is some text... Here is some text... Here is some text... Here is some text... Here is some text...
</div>
Sorry about the first line. It was from another script I was writing. Just delete that part.
ASKER
Hi lil_puffball,
The bookmark icon can be anywhere on the page...i'm OK with what u've done...thx a lot...
i've one other doubt...i'll have the buttons for highlight and bookmark on the top frame in my page...and on the bottom frame i might need to show the html from another site...so i won't be able to include the javascript code in that html...would i still be able to do the highlight and bookmark on that html??
Thanks...
The bookmark icon can be anywhere on the page...i'm OK with what u've done...thx a lot...
i've one other doubt...i'll have the buttons for highlight and bookmark on the top frame in my page...and on the bottom frame i might need to show the html from another site...so i won't be able to include the javascript code in that html...would i still be able to do the highlight and bookmark on that html??
Thanks...
ASKER
In such case should i read the contents of the other html and put it inside a div tag in my html(where i have all the javascript code for highlight and bookmark) and display it?? or is there any other way of doing that??..thx...
ASKER
I'm sorry..the html which i need to display on the bottom frame might be on the same network but on a different m/c..i'm not sure whether reading the other html and putting in a div tag in my page would help...what would be the best way of doing that??
Also i've noticed that whenever i change my screen resolution or resize the browser window the position of the text in html changes..so in that case when the user comes to the same html again with a different window size, i would read the x and y co-ordinates of the highlight from database and try to highlight the text..but i would be highlighting different text if i go by x and y location..or i would be placing the bookmark in the wrong place?..i've seen in some sites that the position of the html text does not change even if the screen is resized..do i need to do something like that?..or do i need to store some other value other than x and y co-ordinates??...Pls help..
Also i've noticed that whenever i change my screen resolution or resize the browser window the position of the text in html changes..so in that case when the user comes to the same html again with a different window size, i would read the x and y co-ordinates of the highlight from database and try to highlight the text..but i would be highlighting different text if i go by x and y location..or i would be placing the bookmark in the wrong place?..i've seen in some sites that the position of the html text does not change even if the screen is resized..do i need to do something like that?..or do i need to store some other value other than x and y co-ordinates??...Pls help..
yuvarajr,
You can just replace each document.something with parent.bottomFrameName.doc ument.some thing.
I wasn't going to store the coordinates anyway, I was going to store the cursor position so this isn't a problem.
However, the bookmark will be a problem. Because the bookmark can be anywhere, there is no way to find the cursor position. Should I just take the closest cursor position?
You can just replace each document.something with parent.bottomFrameName.doc
I wasn't going to store the coordinates anyway, I was going to store the cursor position so this isn't a problem.
However, the bookmark will be a problem. Because the bookmark can be anywhere, there is no way to find the cursor position. Should I just take the closest cursor position?
ASKER
Hi lil_puffball,
Are you saying that, since ur storing the cusror position, even if the window is resized i can highlight the right word?..thats what i want...:)
For bookmark, if you could bring the icon as close as possible that would be great...
My concern is that if the user places bookmark near first word in a line and if the window is resized that word would move to some other place and the bookmark would be near the wrong word..thx...
Are you saying that, since ur storing the cusror position, even if the window is resized i can highlight the right word?..thats what i want...:)
For bookmark, if you could bring the icon as close as possible that would be great...
My concern is that if the user places bookmark near first word in a line and if the window is resized that word would move to some other place and the bookmark would be near the wrong word..thx...
I've gotten the location storing done now, but the resizing isn't quite working for some reason. Anyways, here is the new script, the locations are now based on the cursor position rather than the mouse coordinates. :)
<style>button{width:200px; }</style>
<script>
var clr='yellow';
var offset=2,lineHgt;
var hiliText=new Array();
var hiliLoc=new Array();
var hiliDiv=new Array();
var bkmkText=new Array();
var bkmkLoc=new Array();
var bkmkDiv=new Array();
var x,y,cursorX=null,cursorY=n ull;
function initHiliBkmk(){
var sel=document.body.createTe xtRange(), loc;
clearHili(false);
sel.moveToElementText(docu ment.getEl ementById( "content") );sel.coll apse();
for(i=0;i<hiliLoc.length;i ++){
loc=hiliLoc[i].split("-");
sel.moveEnd("character",lo c[1]);sel. moveStart( "character ",loc[0]);
if(sel.text!=""){hili(sel, false);}
}
}
function hili(sel){
if(sel.text==""){alert("Pl ease select some text first!");return;}
if(sel.boundingHeight>line Hgt){
createHili(sel.boundingTop -offset,se l.offsetLe ft-offset, sel.boundi ngWidth-se l.offsetLe ft+sel.bou ndingLeft, lineHgt,cl r);
for(i=1;i<parseInt(sel.bou ndingHeigh t/lineHgt) -1;i++){
createHili(sel.boundingTop -offset+li neHgt*i,se l.bounding Left-offse t,sel.boun dingWidth, lineHgt,cl r);
}
var newsel=sel.duplicate();
while(sel.inRange(newsel)) {
newsel.moveStart("characte r");
}newsel.moveStart("charact er",-1);
createHili(newsel.bounding Top-offset ,sel.bound ingLeft-of fset,newse l.bounding Left-sel.b oundingLef t,lineHgt, clr);
}else{
createHili(sel.boundingTop -offset,se l.bounding Left-offse t,sel.boun dingWidth, sel.boundi ngHeight,c lr);
}
if(arguments.length==1){st oreHili(se l);}
}
function storeHili(sel){
hiliText[hiliText.length]= sel.text;
var tempSel=sel.duplicate(),te mpSel2=sel .duplicate ();
tempSel.collapse();tempSel 2.collapse (false);
hiliLoc[hiliLoc.length]=ge tCursorPos (tempSel)+ "-"+getCur sorPos(tem pSel2);
}
function createHili(t,l,w,h,clr){
var div=document.createElement ("div");
div.style.height=h;div.sty le.width=w ;
div.style.left=l;div.style .top=t;
div.style.position="absolu te";div.st yle.fontSi ze="0px";d iv.style.b ackgroundC olor=clr;d iv.innerHT ML=" " ;div.style .zIndex=-1 ;
document.body.appendChild( div);
hiliDiv[hiliDiv.length]=di v;
}
function clearHili(){
for(i=0;i<hiliDiv.length;i ++){
hiliDiv[i].parentNode.remo veChild(hi liDiv[i]);
}
if(arguments.length==0){hi liText.len gth=0;hili Loc.length =0;hiliDiv .length=0; }
}
function clearBookmarks(){
for(i=0;i<bkmkDiv.length;i ++){
bkmkDiv[i].parentNode.remo veChild(bk mkDiv[i]);
}
bkmkText.length=0;bkmkLoc. length=0;b kmkDiv.len gth=0;
}
function insertBookmark(){
if(!cursorX){alert("Please click somewhere first.");return;}
var text=prompt("Enter the text:","Bookmark "+(bkmkDiv.length+1));
if(!text){return;}
var img=document.createElement ("div");
img.id="bookmark"+bkmkDiv. length;
img.style.position="absolu te";
img.style.top=cursorY;
img.style.left=cursorX;
img.innerHTML="<img src='bookmark.gif' title='"+text+"' height=15 unselectable=on>";
img.unselectable="on";
img.onmousedown=function() {startDrag (this);}
document.body.appendChild( img);
bkmkDiv[bkmkDiv.length]=im g;
bkmkText[bkmkText.length]= text;
snapToText(img,bkmkLoc.len gth);
}
var offL,offT,drag=false,obj;
function startDrag(srcEl){obj=srcEl ;offL=x-ge tLeft(obj) ;offT=y-ge tTop(obj); drag=true; }
function dragObj(){obj.style.left=x -offL;obj. style.top= y-offT;}
function getLeft(obj){return parseInt(obj.style.left);}
function getTop(obj){return parseInt(obj.style.top);}
function init(){
var div=document.createElement ("div");
div.innerHTML="1";
document.body.appendChild( div);
lineHgt=div.offsetHeight;
div.parentNode.removeChild (div);
}
function getCursorPos(sel){
var pos=document.body.createTe xtRange();
pos.moveToElementText(docu ment.getEl ementById( "content") );
pos.collapse();var cnt=0;
while(!sel.inRange(pos)){
pos.move("character");cnt+ +;}
return cnt;
}
function snapToText(img,n){
var start=document.body.create TextRange( );
start.moveToElementText(do cument.get ElementByI d("content "));
var end=start.duplicate();star t.collapse ();end.col lapse(fals e);var newsel=start.duplicate();
var cnt=0,pos=0,l=parseInt(img .style.lef t),t=parse Int(img.st yle.top);
var dif=Math.abs(l-start.bound ingLeft)+M ath.abs(t- start.boun dingTop),t hisDif;
while(!end.inRange(start)) {
start.move("character");cn t++;
thisDif=Math.abs(l-start.b oundingLef t)+Math.ab s(t-start. boundingTo p);
if(thisDif<dif){pos=cnt;di f=thisDif; }
}
newsel.move("character",
pos);
img.style.left=newsel.boun dingLeft-o ffset;
img.style.top=newsel.bound ingTop-off set;
bkmkLoc[n]=getCursorPos(ne wsel);
}
</script>
<body onload="init();"onresize=" initHiliBk mk();" onmousemove="x=event.x;y=e vent.y;if( drag){drag Obj();}" onmouseup="if(drag){drag=f alse;snapT oText(obj, obj.id.cha rAt(obj.id .length-1) );}">
<button onclick="hili(document.sel ection.cre ateRange() );">Hiligh t Text</button>
<button onclick="clearHili();">Cle ar All Highlights</button>
<button onclick="alert(hiliText.jo in('\n---- ---------- -\n'));">G et Highlight Text</button>
<button onclick="alert(hiliLoc.joi n('\n----- ---------- \n'));">Ge t Highlight Positions</button>
<br>
<button onclick="insertBookmark(); ">Insert Bookmark</button>
<button onclick="clearBookmarks(); ">Clear All Bookmarks</button>
<button onclick="alert(bkmkText.jo in('\n---- ---------- -\n'));">G et Bookmark Text</button>
<button onclick="alert(bkmkLoc.joi n('\n----- ---------- \n'));">Ge t Bookmark Positions</button>
<div id=content onclick="cursorX=x;cursorY =y;">
Here is some text... Here is some text... Here is some text... Here is some text... Here is some text...
Here is some text... Here is some text... Here is some text... Here is some text... Here is some text...
Here is some text... Here is some text... Here is some text... Here is some text... Here is some text...
Here is some text... Here is some text... Here is some text... Here is some text... Here is some text...
Here is some text... Here is some text... Here is some text... Here is some text... Here is some text...
Here is some text... Here is some text... Here is some text... Here is some text... Here is some text...
</div>
</body>
<style>button{width:200px;
<script>
var clr='yellow';
var offset=2,lineHgt;
var hiliText=new Array();
var hiliLoc=new Array();
var hiliDiv=new Array();
var bkmkText=new Array();
var bkmkLoc=new Array();
var bkmkDiv=new Array();
var x,y,cursorX=null,cursorY=n
function initHiliBkmk(){
var sel=document.body.createTe
clearHili(false);
sel.moveToElementText(docu
for(i=0;i<hiliLoc.length;i
loc=hiliLoc[i].split("-");
sel.moveEnd("character",lo
if(sel.text!=""){hili(sel,
}
}
function hili(sel){
if(sel.text==""){alert("Pl
if(sel.boundingHeight>line
createHili(sel.boundingTop
for(i=1;i<parseInt(sel.bou
createHili(sel.boundingTop
}
var newsel=sel.duplicate();
while(sel.inRange(newsel))
newsel.moveStart("characte
}newsel.moveStart("charact
createHili(newsel.bounding
}else{
createHili(sel.boundingTop
}
if(arguments.length==1){st
}
function storeHili(sel){
hiliText[hiliText.length]=
var tempSel=sel.duplicate(),te
tempSel.collapse();tempSel
hiliLoc[hiliLoc.length]=ge
}
function createHili(t,l,w,h,clr){
var div=document.createElement
div.style.height=h;div.sty
div.style.left=l;div.style
div.style.position="absolu
document.body.appendChild(
hiliDiv[hiliDiv.length]=di
}
function clearHili(){
for(i=0;i<hiliDiv.length;i
hiliDiv[i].parentNode.remo
}
if(arguments.length==0){hi
}
function clearBookmarks(){
for(i=0;i<bkmkDiv.length;i
bkmkDiv[i].parentNode.remo
}
bkmkText.length=0;bkmkLoc.
}
function insertBookmark(){
if(!cursorX){alert("Please
var text=prompt("Enter the text:","Bookmark "+(bkmkDiv.length+1));
if(!text){return;}
var img=document.createElement
img.id="bookmark"+bkmkDiv.
img.style.position="absolu
img.style.top=cursorY;
img.style.left=cursorX;
img.innerHTML="<img src='bookmark.gif' title='"+text+"' height=15 unselectable=on>";
img.unselectable="on";
img.onmousedown=function()
document.body.appendChild(
bkmkDiv[bkmkDiv.length]=im
bkmkText[bkmkText.length]=
snapToText(img,bkmkLoc.len
}
var offL,offT,drag=false,obj;
function startDrag(srcEl){obj=srcEl
function dragObj(){obj.style.left=x
function getLeft(obj){return parseInt(obj.style.left);}
function getTop(obj){return parseInt(obj.style.top);}
function init(){
var div=document.createElement
div.innerHTML="1";
document.body.appendChild(
lineHgt=div.offsetHeight;
div.parentNode.removeChild
}
function getCursorPos(sel){
var pos=document.body.createTe
pos.moveToElementText(docu
pos.collapse();var cnt=0;
while(!sel.inRange(pos)){
pos.move("character");cnt+
return cnt;
}
function snapToText(img,n){
var start=document.body.create
start.moveToElementText(do
var end=start.duplicate();star
var cnt=0,pos=0,l=parseInt(img
var dif=Math.abs(l-start.bound
while(!end.inRange(start))
start.move("character");cn
thisDif=Math.abs(l-start.b
if(thisDif<dif){pos=cnt;di
}
newsel.move("character",
pos);
img.style.left=newsel.boun
img.style.top=newsel.bound
bkmkLoc[n]=getCursorPos(ne
}
</script>
<body onload="init();"onresize="
<button onclick="hili(document.sel
<button onclick="clearHili();">Cle
<button onclick="alert(hiliText.jo
<button onclick="alert(hiliLoc.joi
<br>
<button onclick="insertBookmark();
<button onclick="clearBookmarks();
<button onclick="alert(bkmkText.jo
<button onclick="alert(bkmkLoc.joi
<div id=content onclick="cursorX=x;cursorY
Here is some text... Here is some text... Here is some text... Here is some text... Here is some text...
Here is some text... Here is some text... Here is some text... Here is some text... Here is some text...
Here is some text... Here is some text... Here is some text... Here is some text... Here is some text...
Here is some text... Here is some text... Here is some text... Here is some text... Here is some text...
Here is some text... Here is some text... Here is some text... Here is some text... Here is some text...
Here is some text... Here is some text... Here is some text... Here is some text... Here is some text...
</div>
</body>
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Hi lil_puffball,
Thanks a lot..:))..
i understood that i need to call initHiliBkmk(true) function to highlight and bookmark when i load the page. But i dont understand why you want me to pass the true parameter to that function.. i guess it should work without that..is that right??..
Thanks a lot..:))..
i understood that i need to call initHiliBkmk(true) function to highlight and bookmark when i load the page. But i dont understand why you want me to pass the true parameter to that function.. i guess it should work without that..is that right??..
ASKER
Also i'll accept your reply once you clarify this doubt..
The reason is because the function is also called when the page is resized. When you load the page, you want to create the bookmarks, but when you resize, all you want to do is change the position, not create new divs. So the true parameter just tells the function that the bookmarks should be created. :)
ASKER
got it..thx a lot..:)
Glad to help. Thanks for the points. :)
http://www.interactivetools.com/products/htmlarea/