Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 01-28-2005, 05:44 PM   PM User | #1
homerUK
Regular Coder

 
Join Date: Nov 2002
Location: Manchester, UK
Posts: 533
Thanks: 4
Thanked 1 Time in 1 Post
homerUK is an unknown quantity at this point
code for IE - can it be converted for NS?

Hi,
I've found the following code:

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..
__________________
www.mattfacer.com
homerUK is offline   Reply With Quote
Old 01-28-2005, 06:36 PM   PM User | #2
Willy Duitt
Banned

 
Join Date: Sep 2003
Posts: 3,620
Thanks: 0
Thanked 0 Times in 0 Posts
Willy Duitt is an unknown quantity at this point
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...

Code:
<!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
Willy Duitt is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 04:40 AM.


Advertisement
Log in to turn off these ads.