...

View Full Version : code for IE - can it be converted for NS?



homerUK
01-28-2005, 06:44 PM
Hi,
I've found the following code:



function hiliteToBold(txtArea){
txtAreaName = txtArea.name;
txtRange = document.all[txtArea].createTextRange();
txtContainer = txtRange.parentElement().name;

objRange = document.selection.createRange();
hiliteTxt = objRange.text;
toBoldTxt = "<B>" + hiliteTxt + "</B>";

if(hiliteTxt != ""){
objRange.text = toBoldTxt;
}
}


which will add the "<B>" tags around a highlighted text string in a message box...... it works ok with IE, but in firefox and NS it wont work....
are there similar functions for NS/Firefox?

thanks..

Willy Duitt
01-28-2005, 07:36 PM
Here is a copy of a cross-browser script that Vladdy once posted...
Please properly credit Vladdy @ http://www.vladdy.net/WebDesign/ if you use this routine on your pages...



<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-gb">
<head>
<meta http-equiv="Content-type" content="text/html; charset=US-ASCII" />
<title>Textarea Pseudo-Tag Insertion Demo</title>
<script type="text/javascript">
// Written By: Vladdy @ http://www.vladdy.net/WebDesign/
function getSelection(ta)
{ var bits = [ta.value,'','',''];
if(document.selection)
{ var vs = '#$%^%$#';
var tr=document.selection.createRange()
if(tr.parentElement()!=ta) return null;
bits[2] = tr.text;
tr.text = vs;
fb = ta.value.split(vs);
tr.moveStart('character',-vs.length);
tr.text = bits[2];
bits[1] = fb[0];
bits[3] = fb[1];
}
else
{ if(ta.selectionStart == ta.selectionEnd) return null;
bits=(new RegExp('([\x00-\xff]{'+ta.selectionStart+'})([\x00-\xff]{'+(ta.selectionEnd - ta.selectionStart)+'})([\x00-\xff]*)')).exec(ta.value);
}
return bits;
}

function matchPTags(str)
{ str = ' ' + str + ' ';
ot = str.split(/\[[B|U|I].*?\]/i);
ct = str.split(/\[\/[B|U|I].*?\]/i);
return ot.length==ct.length;
}

function addPTag(ta,pTag)
{ bits = getSelection(ta);
if(bits)
{ if(!matchPTags(bits[2]))
{ alert('\t\tInvalid Selection\nSelection contains unmatched opening or closing tags.');
return;
}
ta.value = bits[1] + '[' + pTag + ']' + bits[2] + '[/' + pTag + ']' + bits[3];
}
}
</script>
<style type="text/css">
</style>
</head>

<body>
<h1>Textarea Pseudo-Tag Insertion Demo</h1>
<p>Select text in the textarea. Then press desired button.</p>
<fieldset>
<button onclick="addPTag(document.getElementById('text'),'B')"><b>Bold</b></button>
<button onclick="addPTag(document.getElementById('text'),'I')"><i>Italic</i></button>
<button onclick="addPTag(document.getElementById('text'),'U')"><u>Underline</u></button>
</fieldset>
<textarea id="text" rows="20" cols="80">
Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Duis suscipit ultricies tortor. Morbi auctor, sapien ut posuere porta, magna nisl tempus leo, ut dictum ipsum sem non odio. Pellentesque pharetra, velit quis molestie lacinia, quam sem interdum urna, vel aliquet dolor tellus eget massa. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Vivamus in nibh. Donec non purus. Etiam ut sem sed quam dapibus porta. Nam consequat congue dolor. Morbi id felis. Aliquam nibh. Etiam sollicitudin, augue accumsan cursus adipiscing, est justo imperdiet neque, in gravida wisi neque at ante. Nulla at tortor. Donec elementum tristique pede. Mauris in erat. Nullam posuere sem et felis. Aliquam consequat bibendum ante. Sed vel magna in urna aliquet aliquet. Sed feugiat, lectus nec fringilla dignissim, elit diam tristique arcu, eu consequat erat augue eu nibh.</textarea>
</body>
</html>


.....Willy



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum