www.webdeveloper.com
Results 1 to 2 of 2

Thread: Underline Bookmarklet

  1. #1
    Join Date
    Oct 2013
    Posts
    1

    Underline Bookmarklet

    I am developing an underline bookmarklet. It was designed to work in any text box but it only seems to work in Yahoo mail. I have no idea why. I have included it below. Line breaks and comments have been added. In reality it is one line that goes in the address of the bookmark. When it works inputString has a value. When it doesn't work (which is everywhere but Yahoo mail) inputString is blank.
    It is available for testing at http://dmmarks.com/bookmarklets.html

    Code:
    javascript:
    /* function to convert HTML entities to text */
    function%20get_ents(str)
    {var%20temp=document.createElement('pre');
    temp.innerHTML=str;
    return%20temp.firstChild.nodeValue;};
    
    /* get the selected text */
    inputString=document.getSelection().getRangeAt(0); <----- problem seems to be here
    inputChar='';
    outputChar='';
    
    /* convert to string so length will work */
    selectedText%20=%20inputString.toString();
    
    /* add the underline charaters */
    for(i=0;i<selectedText.length;i++)
    {outputChar=outputChar+selectedText.charAt(i).concat('%CC%B2')};
    
    /* replace the selected text */
    inputString.deleteContents();
    inputString.insertNode(document.createTextNode(get_ents(outputChar)));
    void(0);

  2. #2
    Join Date
    Nov 2002
    Location
    Flint, Michigan, USA
    Posts
    588
    You need a bit more work to properly get the selected text. I use this subroutine:
    Code:
    //  getselec.js  Get selected text
    //  Written:  2013-05-30 by James Alarie
    //    http://spruce.flint.umich.edu/~jalarie/
    //  Title=Pick up highlighted text from the web page.
    //  Keywords=select,highlight,text.
    //
    //  Functions:
    //    GetSelected(What)
    //      What      optional      The prompt to use if nothing is selected.
    //      if nothing is selected and "What" is null, a null value is returned.
    
      function GetSelected(What) {
        var Value;
        if (window.getSelection) {
          Value=window.getSelection();
        } else {
          if (document.getSelection) {
            Value=document.getSelection();
          } else {
            Value=document.selection.createRange().text;
          }
        }
        if (Value == '') {
          if (What) {
            Value=prompt(What,'');
          }
        }
        return Value+'';
      } // GetSelected

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles