How to get BB code script to work in mozilla?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • azegurb
    New Member
    • Sep 2009
    • 32

    How to get BB code script to work in mozilla?

    Hi all,
    I have small bbcode script that works perfectly in IE. but in mozilla it doesn't work
    here is script
    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" 
        "http://www.w3.org/TR/html4/strict.dtd" 
        > 
    <html> 
    <head> 
        <head> 
    <!-- ... --> 
    <script language="JavaScript" type="text/javascript"> 
    var v=2; 
    <!-- 
    function createTag(tagId,areaId) { 
    //   var x=tagId; 
         
        
       var txtArea = document.getElementById(areaId); 
        var aSelection = document.selection.createRange().text; 
        var range = txtArea.createTextRange(); 
    
    
            if(aSelection) { 
                // Add tags around selection 
                document.selection.createRange().text = '['+tagId+']' + aSelection + '[/'+tagId+']'; 
                txtArea.focus(); 
                range.move('textedit'); 
                //range.select(); 
            } 
                     
            else { 
                //var oldStringLength = range.text.length + '['+tagId+']'.length; 
                            if(v==2){ 
                txtArea.value += '['+tagId+']'; 
                //txtArea.value += '[/'+tagId+']'; 
                            v=1; 
                            } 
                            else { 
                                txtArea.value += '[/'+tagId+']'; 
                                v=2; 
                            } 
                txtArea.focus(); 
                range.move('character',oldStringLength); 
                range.collapse(false); 
                //range.select(); 
            } 
        if ((txtArea.selectionStart) || (txtArea.selectionStart == '0')) { 
                 
                var startPos = txtArea.selectionStart; 
    
            var endPos = txtArea.selectionEnd; 
    
            txtArea.value = txtArea.value.substring(0, startPos)+'az'+ txtArea.value.substring(endPos, txtArea.value.length); 
    
    
                 
                return txtArea.value 
                 
                 
            } 
            
    } 
    //--> 
    </script> 
    </head> 
    <body> 
    <!-- ... --> 
        <form onSubmit="return true;"> 
            <textarea id="txtA" rows=20 cols=40></textarea> 
        </form> 
        <a href="javascript<b></b>:createTag('b','txtA')"><strong>B</strong></a> 
            <a href="javascript<b></b>:createTag('i','txtA')"><strong>i</strong></a> 
    <!-- ... --> 
    </body> 
    </body> 
    </html>
    Can anyone help me so that it works both in IE and Mozilla Firefox
    Thanks for advance
  • Dormilich
    Recognized Expert Expert
    • Aug 2008
    • 8694

    #2
    the Range object in IE and the Range object in Mozilla are fundamentally different (ref.) you need to use/develop a cross-browser version for your code.
    Last edited by Dormilich; Jan 30 '11, 08:23 AM.

    Comment

    • Ketrid
      New Member
      • Feb 2011
      • 4

      #3
      Get rid of the bold tags in the middle of your javascript call, look before the function:
      <a href="javascrip t<b></b>:createTag('b ','txtA')"><str ong>B</strong></a>

      To simplify the inter-browser compatibility, use jquery and let the API do the work of figuring out those different commands for you.

      Comment

      Working...