Selecting text

  • MichaelJoel
  • Newbie
  • Newbie
  • MichaelJoel
  • Posts: 7

Post 3+ Months Ago

I am using DHTML to create contect windows.

My script is needing to copy to clipboard any text selected by user (I have this), but if the user hasn't selected text then I want to select all the text in the browser and copy it.

I cant figure out how to select all the text.

Mike
  • Anonymous
  • Bot
  • No Avatar
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post 3+ Months Ago

  • MichaelJoel
  • Newbie
  • Newbie
  • MichaelJoel
  • Posts: 7

Post 3+ Months Ago

Should I assume, there isnt a way?
  • MichaelJoel
  • Newbie
  • Newbie
  • MichaelJoel
  • Posts: 7

Post 3+ Months Ago

I figured it out!
:)

Thanks
  • ATNO/TW
  • Super Moderator
  • Super Moderator
  • User avatar
  • Posts: 23456
  • Loc: Woodbridge VA

Post 3+ Months Ago

Sorry nobody got to this to help you before you got it. I did read this earlier but had no answer. We encourage you to provide the solution in case someone else reading this in the future reads this and has the same problem. Posts at OZZU stay around a looooong time.
  • MichaelJoel
  • Newbie
  • Newbie
  • MichaelJoel
  • Posts: 7

Post 3+ Months Ago

Solution was

Code: [ Select ]
// GRAB ANY TEXT THE USER HAS SELECTED
var CheckForSelection = window.external.menuArguments.document.selection.createRange().text;


// IF THERE WAS NO SELECTED TEXT THEN COPY THE ENTIRE PAGE (AS TEXT)
if(CheckForSelection=="") {

// GET ALL TEXT ON PAGE
var PageText = window.external.menuArguments.document.body.createTextRange( ).text;
// COPY IT TO CLIPBOARD
window.clipboardData.setData("Text",PageText);

// THE FIRST CHECK SHOWED TEXT IS SELECTED SO
} else {

// GET SELECTED TEXT
var SelectedText = window.external.menuArguments.document.selection.createRange().text;
// COPY IT TO CLIPBOARD
window.clipboardData.setData("Text",SelectedText);

}
  1. // GRAB ANY TEXT THE USER HAS SELECTED
  2. var CheckForSelection = window.external.menuArguments.document.selection.createRange().text;
  3. // IF THERE WAS NO SELECTED TEXT THEN COPY THE ENTIRE PAGE (AS TEXT)
  4. if(CheckForSelection=="") {
  5. // GET ALL TEXT ON PAGE
  6. var PageText = window.external.menuArguments.document.body.createTextRange( ).text;
  7. // COPY IT TO CLIPBOARD
  8. window.clipboardData.setData("Text",PageText);
  9. // THE FIRST CHECK SHOWED TEXT IS SELECTED SO
  10. } else {
  11. // GET SELECTED TEXT
  12. var SelectedText = window.external.menuArguments.document.selection.createRange().text;
  13. // COPY IT TO CLIPBOARD
  14. window.clipboardData.setData("Text",SelectedText);
  15. }




I know this could be shortened, but for the purposes I am using it, it must be copied to clipboard only when ready.

Otherwise you could shorten it to:

Code: [ Select ]
// GRAB ANY TEXT THE USER HAS SELECTED
var CheckForSelection = window.external.menuArguments.document.selection.createRange().text;
// COPY IT TO CLIPBOARD
window.clipboardData.setData("Text",CheckForSelection);


// IF THERE WAS NO SELECTED TEXT THEN COPY THE ENTIRE PAGE (AS TEXT)
if(CheckForSelection=="") {

// GET ALL TEXT ON PAGE
var PageText = window.external.menuArguments.document.body.createTextRange( ).text;
// COPY IT TO CLIPBOARD
window.clipboardData.setData("Text",PageText);
}
  1. // GRAB ANY TEXT THE USER HAS SELECTED
  2. var CheckForSelection = window.external.menuArguments.document.selection.createRange().text;
  3. // COPY IT TO CLIPBOARD
  4. window.clipboardData.setData("Text",CheckForSelection);
  5. // IF THERE WAS NO SELECTED TEXT THEN COPY THE ENTIRE PAGE (AS TEXT)
  6. if(CheckForSelection=="") {
  7. // GET ALL TEXT ON PAGE
  8. var PageText = window.external.menuArguments.document.body.createTextRange( ).text;
  9. // COPY IT TO CLIPBOARD
  10. window.clipboardData.setData("Text",PageText);
  11. }


Mike
  • ATNO/TW
  • Super Moderator
  • Super Moderator
  • User avatar
  • Posts: 23456
  • Loc: Woodbridge VA

Post 3+ Months Ago

Thanks for the solution! *glad you got it.
  • Skybin
  • Graduate
  • Graduate
  • Skybin
  • Posts: 104
  • Loc: Where the soy and cornfields grow...

Post 3+ Months Ago

I've never really gotten into usign DHTML for fear of browser support. Is it something that requires a pluging like flash, or something that someone can turn off like javascript?
  • ATNO/TW
  • Super Moderator
  • Super Moderator
  • User avatar
  • Posts: 23456
  • Loc: Woodbridge VA

Post 3+ Months Ago

DHTML is essentially a combination of good javascript and CSS coding. If javascript is disabled in a browser by the user then DHTML won't work.

There are also differences in javascript code between Mozilla based browsers and MSIE that need to be accounted for. Unfortunately this guy is still redisigning his place or forgot about it, but his archived tutes are excellent:

http://www.htmlguru.com/

Post Information

  • Total Posts in this topic: 8 posts
  • Users browsing this forum: No registered users and 52 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.