Click to See Complete Forum and Search --> : window.getSelection()


stovellp
12-24-2003, 05:02 AM
Howdy,

I have the following javascript code, which is supposed to help with some forums I am creating. When a 'Bold' button is clicked, the function will get the selected text and put a |B| around it.


function bold()
{
var userselection = "";
if (window.getSelection) userselection = window.getSelection();
else if (document.getSelection) userselection = document.getSelection();
else if (document.selection) userselection = document.selection.createRange().text;

if (userselection == "")
{
var Textin = prompt("Enter the text you want to appear as bold:", "");
document.bheardform.message.value = document.bheardform.message.value + "|b|" + Textin + "|/b|";
document.bheardform.message.focus();
}
else
{
NewContent = document.bheardform.message.value;
NewContent = NewContent.replace(userselection, "|B|" + userselection + "|/B|");
document.bheardform.message.value = NewContent;
}
}

The code does work, but its limited. It gets the selected text, and replaces the occurence of that text in the message with the same text but with a |B|/|/B|around it. The problem is, if there are two occurences of the selected string in the message, the first is the one to get wrapped with the |B| tags, not neccesarily the selected one.

So, rather than using window.getSelection() (and its variants), are there functions I can use to get the position (both start and end) of the selected text, so I can replace it accordingly?

And what function (example please :() would I use to replace a substring of text?

Thanks for any help, I really appreciate it!

olerag
12-24-2003, 09:24 AM
1. I can only get this code to work in IE6 (not NN7) and am
using an additional button to fire your "bold()" function.
I do not get the proper "text" return if I use an "onSelect"
event in the "text" object. Is this correct or should I be
implementing the code differently.

2. If the action works fine for you and you are able to
capture and place the desired string of text in the
"NewContent" variable, then iterate thru the string of the
text object and replace all instances of the captured string
with your enhancement tags.

Would you like a string iteration code snippet for this or have
I not grasped what your after?

olerag
12-24-2003, 10:43 AM
Here's some sample code that will handle all occurances
of your string search. Again, this only appears to work in
IE6 - the "userSelection" is always null in NN7 consequently
you'll always get a prompt window with that browser.

<html>
<head>
<script type="text/javascript">
function clearField() {
document.forms[0].field1.value = "";
document.forms[0].field1.focus();
}

function makeBold() {
var userSelection = "";
var fieldVal = document.forms[0].field1.value;

if (window.getSelection) {
userSelection = window.getSelection();
}
else if (document.getSelection) {
userSelection = document.getSelection();
}
else if (document.selection) {
userSelection = document.selection.createRange().text;
}

if (userSelection == "") {
var textIn = "";
textIn = prompt("Enter the text you want to appear as bold:", "");
if (textIn.length > 0) {
document.forms[0].field1.value = replaceAll(fieldVal,textIn);
}
}
else {
document.forms[0].field1.value = replaceAll(fieldVal,userSelection);
}
document.forms[0].field1.focus();
}

function replaceAll(str,searchStr) {
var replaceStr = "|B|" + searchStr + "|/B|";
var re = new RegExp(searchStr, "g");
return(str.replace(re, replaceStr));
}
</script>
</head>
<body>
<center>
<b>Text Replacement Example</b>
<form>
<input type="text" name="field1" size="50"><br>
<input type="button" value="Bold" onClick="makeBold()">
<input type="button" value="Clear" onClick="clearField()">
</form>
<hr>
</center>
</body>
</html>