Selecting text

  • MichaelJoel
  • Newbie
  • Newbie
  • No Avatar
  • Joined: Aug 26, 2004
  • Posts: 7
  • Status: Offline

Post August 26th, 2004, 7:20 am

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
  • Joined: 25 Feb 2008
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post August 26th, 2004, 7:20 am

  • MichaelJoel
  • Newbie
  • Newbie
  • No Avatar
  • Joined: Aug 26, 2004
  • Posts: 7
  • Status: Offline

Post August 26th, 2004, 4:57 pm

Should I assume, there isnt a way?
  • MichaelJoel
  • Newbie
  • Newbie
  • No Avatar
  • Joined: Aug 26, 2004
  • Posts: 7
  • Status: Offline

Post August 26th, 2004, 6:14 pm

I figured it out!
:)

Thanks
  • ATNO/TW
  • Super Moderator
  • Super Moderator
  • User avatar
  • Joined: May 28, 2003
  • Posts: 23404
  • Loc: Woodbridge VA
  • Status: Offline

Post August 26th, 2004, 6:32 pm

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.
"There's no place like 127.0.0.1 except for ::1."
Alexandria Networks. Leader in IT consulting for associations/non-profits, and small to medium sized businesses around the northern Virginia and Washington D.C. metro area.
  • MichaelJoel
  • Newbie
  • Newbie
  • No Avatar
  • Joined: Aug 26, 2004
  • Posts: 7
  • Status: Offline

Post August 26th, 2004, 7:17 pm

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
  • Joined: May 28, 2003
  • Posts: 23404
  • Loc: Woodbridge VA
  • Status: Offline

Post August 26th, 2004, 7:24 pm

Thanks for the solution! *glad you got it.
"There's no place like 127.0.0.1 except for ::1."
Alexandria Networks. Leader in IT consulting for associations/non-profits, and small to medium sized businesses around the northern Virginia and Washington D.C. metro area.
  • Skybin
  • Graduate
  • Graduate
  • No Avatar
  • Joined: Apr 05, 2004
  • Posts: 104
  • Loc: Where the soy and cornfields grow...
  • Status: Offline

Post August 26th, 2004, 7:32 pm

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
  • Joined: May 28, 2003
  • Posts: 23404
  • Loc: Woodbridge VA
  • Status: Offline

Post August 26th, 2004, 7:33 pm

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/
"There's no place like 127.0.0.1 except for ::1."
Alexandria Networks. Leader in IT consulting for associations/non-profits, and small to medium sized businesses around the northern Virginia and Washington D.C. metro area.

Post Information

  • Total Posts in this topic: 8 posts
  • Users browsing this forum: No registered users and 102 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
 
 

© 2011 Unmelted, LLC. Ozzu® is a registered trademark of Unmelted, LLC.