Topic: Ghosts in the machine: Removed nodes still in the way when making another text selection (Page 1 of 1) |
|
|---|---|
|
Nervous Wreck (II) Inmate From: Portland Oregon |
posted 04-06-2009 07:09
I'm trying to learn a few things about javascript that I've ignored up til this point. Text selection! code: <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Selected Text Position</title>
<style>
.red{color:red;}
.orange{color:orange;}
</style>
<script>
var count=0;
//function searchword(){
var working=false;
function searchword(evt) {
if (working==true){
if (!evt) { evt = window.event; }
var srcText = null;
if (navigator.appName!='Microsoft Internet Explorer') {
var t = document.getSelection();
srcText = evt.target.innerHTML;
findPos(srcText, t);
}
else {
srcText = evt.srcElement.innerHTML;
var t = document.selection.createRange();
if(document.selection.type == 'Text' && t.text>'') {
document.selection.empty();
findPos(srcText, t.text);
}
}
}
function findPos(srcText, text) {
var spos = srcText.indexOf (text);
var epos = spos + text.length -1;
window.status = 'Start Position ' + spos + ' End Position ' + epos;
//alert ('Start Position ' + spos + '\n End Position ' + epos);
working=false;
var workingpic=document.getElementById("workingpic");
workingpic.src="begin.jpg";
var range = window.getSelection().getRangeAt(0);
var range2 = window.getSelection().getRangeAt(0)
//alert(range);
var readout=document.getElementById("readout");
var dummy = document.createElement("span");
dummy.setAttribute('Id','dummy');
range.insertNode(dummy);
var prepic=document.createElement("img");
prepic.setAttribute('Id','prepic');
prepic.src="start.png";
var endpic=document.createElement("img");
endpic.setAttribute('Id','endpic');
endpic.src="end.png";
var box = document.getBoxObjectFor(dummy);
var x = box.x, y = box.y;
var endx=box.x+(epos);
var endy=box.y+(epos);
count+=1
readout.innerHTML+="<span class=red>selection "+count+": </span><span class=orange>"+ range +"</span><br>";
dummy.innerHTML+="<big><big><span class=red>"+count+"</span><big><big>";
dummy.appendChild(prepic);
range.collapse(false);
range.insertNode(endpic);
setTimeout("removeSpan()",2000)
//alert(x);
//alert(y);
}
}
function removeSpan()
{
//alert('hi there');
var dummy=document.getElementById("dummy");
var prepic=document.getElementById("prepic");
var endpic=document.getElementById("endpic");
prepic.parentNode.removeChild(prepic);
endpic.parentNode.removeChild(endpic);
dummy.parentNode.removeChild(dummy);
/*
dummy.removeChild(prepic);
dummy.parentNode.removeChild(endpic);
dummy.parentNode.removeChild(dummy);
removeChildNodes(dummy);
removeChildNodes(range);
*/
}
function removeChildNodes(parent){
while(parent.hasChildNodes()){
parent.removeChild(parent.childNodes[0])
}
}
</script>
</head>
<body onMouseup="searchword(event);">
<h1>Selected Text Position </h1><br><p>Use the select box to begin selection counting. <br>The readout will follow when you lift the mouse.</p>
<img id="workingpic" src="begin.jpg" onclick="javascript:workingpic.src='ready.jpg';working=true;" alt="Click here to begin"><br>
Hello this is a Javascript function to get the start and end position of highlighted text on a page <br> Click the box to begin. Select text and lift mouse to get information about the start and end position. Watch the status bar.
<div id="readout"></div>
</body>
</html> |
|
Nervous Wreck (II) Inmate From: United States |
posted 04-06-2009 17:14
I think the problem that's happening is, when you insert the images, Firefox breaks up the text nodes. When the images are rejoined, the text nodes are never reassembled, causing oddities. |
|
Obsessive-Compulsive (I) Inmate From: |
posted 04-06-2009 17:21
I haven't looked into your script to see where exactly it's going wrong, but I would guess that it's getting hung up on the extra text nodes created. |
|
Nervous Wreck (II) Inmate From: Portland Oregon |
posted 04-06-2009 17:26
thank you so much. I'll give that a try. And thanks for explaining what you think the problem is. You're a huge help. |
|
Nervous Wreck (II) Inmate From: |
posted 05-31-2011 11:08
Edit TP: spam removed
|