SitePoint Sponsor

User Tag List

Results 1 to 17 of 17
  1. #1
    SitePoint Enthusiast
    Join Date
    Nov 2006
    Posts
    99
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    window.content... problem in Mozilla

    Hi!

    I'm new to javascript. I have this code which for some reasons doesn't work in Mozilla firefox 2.0.0.3:

    <script type="text/javascript">
    function select() {
    var SelectedString = window.content.getSelection();
    document.write(SelectedString)
    }
    </script>
    </head>
    <body>
    <form>
    <input type="button" name="bold" value="bold" onclick="select()"><br>
    <textarea cols='10' rows='3' name='trazi'></textarea>
    </form>

    If I replace window.content.getSelection() with document.selection.createRange().text it's working only in IE6&7.

    Any ideas?
    Thanks in advance.

    Bosko

  2. #2
    SitePoint Wizard Pepejeria's Avatar
    Join Date
    Jan 2005
    Location
    Too far up north
    Posts
    1,566
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    IE and Firefox has different implementations when it comes to ranges. Firefox implementation can be read here, and IE's here.

  3. #3
    I meant that to happen silver trophybronze trophy Raffles's Avatar
    Join Date
    Sep 2005
    Location
    Tanzania
    Posts
    4,662
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    And use Object Detection.

  4. #4
    SitePoint Enthusiast
    Join Date
    Nov 2006
    Posts
    99
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hm! Thanks, but this is too much for me... Since I'm javascript newbie, I cant follow these pages(for now). I know that I have to do some reading and learning. Could you give some example?
    Thanks

  5. #5
    SitePoint Wizard Pepejeria's Avatar
    Join Date
    Jan 2005
    Location
    Too far up north
    Posts
    1,566
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Sorry, but dealing with ranges is not a simple thing. You just need to look into it.

  6. #6
    SitePoint Enthusiast
    Join Date
    Nov 2006
    Posts
    99
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ok. If I find a solution I'll post it.

  7. #7
    SitePoint Wizard silver trophy kyberfabrikken's Avatar
    Join Date
    Jun 2004
    Location
    Copenhagen, Denmark
    Posts
    6,157
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Don't use document.write. Use getElementById() to get an element, and then use innerHTML to inject HTML into the page.

  8. #8
    SitePoint Enthusiast
    Join Date
    Nov 2006
    Posts
    99
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I've looked at getElementById() tutorial but it doesn't work for me. What I want is to select part of a string from textarea and then by pressing a button to extract only selected string. getElementById() is to 'select' a tag from html.

    It's funny that in Mozilla my code which I post as first is working only when selected string is outside of textarea (printed on screen). In IE everything is working.

    Anyway. Thanks.

  9. #9
    CSS & JS/DOM Adept bronze trophy
    Join Date
    Mar 2005
    Location
    USA
    Posts
    5,482
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    kyberfabrikken is saying to use DOM methods and the innerHTML property to output the text.

    Mozilla handles text selection outside teaxtarea and input elements differently from inside them.

    Check these out:
    How can I manipulate the selection and the caret in an input type="text" element in Mozilla browsers and IE/Win?
    http://www.alexking.org/blog/2003/06...ng-javascript/
    http://www.massless.org/mozedit/
    http://parentnode.org/javascript/wor...rsor-position/
    We miss you, Dan Schulz.
    Learn CSS. | X/HTML Validator | CSS validator
    Dynamic Site Solutions
    Code for Firefox, Chrome, Safari, & Opera, then add fixes for IE, not vice versa.

  10. #10
    SitePoint Enthusiast
    Join Date
    Nov 2006
    Posts
    99
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well, as Pepejeria sad it's not easy I think I found solution in link http://www.massless.org/mozedit/

    Now It will take me some time to understand the code.

    Thanks to all for your time and help.

    Bosko

  11. #11
    SitePoint Enthusiast
    Join Date
    Nov 2006
    Posts
    99
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    As I sad I found it on that link and here is code that I was looking for. This is working only in Mozilla. I did small changes to get what I need:

    <html>
    <head>
    <script language="JavaScript">
    var thisForm;
    function mozWrap(txtarea) {
    var selStart = txtarea.selectionStart;
    var selEnd = txtarea.selectionEnd;
    var ext = (txtarea.value).substring(selStart, selEnd);
    txtarea.value = ext;
    }
    </script>
    </head>
    <body onload="thisForm=document.frmTest;">
    <form name="frmTest" id="frmTest">
    <textarea name="T" cols="20" rows="3">blahblahblah</textarea><br>
    <input type="button" value="Extract selection" onclick="mozWrap(thisForm.T);">
    </form>
    </body>
    </html>

  12. #12
    SitePoint Wizard Pepejeria's Avatar
    Join Date
    Jan 2005
    Location
    Too far up north
    Posts
    1,566
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    What is that you want to achieve? You basically just clear the field now with the above example.

  13. #13
    SitePoint Enthusiast
    Join Date
    Nov 2006
    Posts
    99
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well, no.
    Quote Originally Posted by bosko View Post
    What I want is to select part of a string from textarea and then by pressing a button to extract only selected string.
    and put it back in textarea.
    Example above is doing exactly that.

  14. #14
    SitePoint Wizard Pepejeria's Avatar
    Join Date
    Jan 2005
    Location
    Too far up north
    Posts
    1,566
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ah, sorry, I just pressed the button without selecting something.


  15. #15
    CSS & JS/DOM Adept bronze trophy
    Join Date
    Mar 2005
    Location
    USA
    Posts
    5,482
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by bosko View Post
    As I sad I found it on that link and here is code that I was looking for. This is working only in Mozilla.
    So you don't test in Opera?

    You need the function that uses the IE way of doing that too.
    We miss you, Dan Schulz.
    Learn CSS. | X/HTML Validator | CSS validator
    Dynamic Site Solutions
    Code for Firefox, Chrome, Safari, & Opera, then add fixes for IE, not vice versa.

  16. #16
    SitePoint Enthusiast
    Join Date
    Nov 2006
    Posts
    99
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Kravvitz View Post
    So you don't test in Opera?
    Today I did and same code is working.

    Quote Originally Posted by Kravvitz View Post
    You need the function that uses the IE way of doing that too.
    For IE it wasn't a problem from the start (see my first post). Problem was only in Mozilla. But here is a complete function for IE too which is much simplier:

    <html>
    <head>
    <script language="JavaScript" type="text/javascript">
    function ieWrap() {
    var ext = document.selection.createRange().text;
    document.fname.tname.value = ext
    }
    </script>
    </head>
    <body>
    <form name="fname">
    <input type="button" value="Extract it" onclick="ieWrap()"><br>
    <textarea cols='10' rows='3' name='tname'>adsfgasdfgsdfgsdfg</textarea>
    </form>
    </body>
    </html>

  17. #17
    SitePoint Enthusiast
    Join Date
    Nov 2006
    Posts
    99
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Just to remind: before pressing 'Extract it' you should select something...


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •