GetSelection code testing

26 views
Skip to first unread message

Dimitris Papageorgiou

unread,
Jun 14, 2011, 1:15:50 PM6/14/11
to fir...@googlegroups.com
I want to test a GetSelection code.

The code is the following:
function GetSelectedText () {
            if (window.getSelection) {  // all browsers, except IE before version 9
                var range = window.getSelection ();                                        
                alert (range.toString ());
            }} 
This code assumes that a text segment is selected in a webpage.
The console seems that it does NOT communicate/interact with the webpage where it is opened.

How am i going to validate the above code?

I wrote the code in the console, selected text from the webpage, pressed the run command and what i get is undefined.

I do not think the code is wrong, it is just that the firebug console seems not to communicate with the webpage where it is opened.
I am talking only for the console, no problem with css or html.

Thanks.

Lil Devil

unread,
Jun 15, 2011, 1:48:00 AM6/15/11
to Firebug
I tried your code, except for the function wrapper and saw the same
error. Then I removed "var" and it worked correctly. Not sure why this
is. Hopefully someone smarter than I can explain this.

> if (window.getSelection) {  // all browsers, except IE before version 9

Dimitris Papageorgiou

unread,
Jun 15, 2011, 3:39:49 AM6/15/11
to fir...@googlegroups.com
Yes it worked on me also.

Me either cannot understand why removing the var makes it work.

John J Barton

unread,
Jun 15, 2011, 12:52:46 PM6/15/11
to Firebug


On Jun 14, 10:15 am, Dimitris Papageorgiou <foryou1...@gmail.com>
wrote:
> I want to test a GetSelection code.
>
> The code is the following:
>
> function GetSelectedText () {
>             if (window.getSelection) {  // all browsers, except IE before version 9                
var range = window.getSelection ();                
                       
// I assume you meant this statement to be on a separate line.
>                 alert (range.toString ());
>             }}
> This code assumes that a text segment is selected in a webpage.
> The console seems that it does NOT communicate/interact with the webpage where it is opened.

I'm unsure what makes you say this.

>
> How am i going to validate the above code?

The code above is a function declaration. When you put it in the
console it evaluates to |undefined| because the expression has no
value.

>
> I wrote the code in the console, selected text from the webpage, pressed the run command and what i get is *undefined*.

I believe this is the correct result.

If you want to run the function you have to call it. For that you can
use:
GetSelectedText();
or
(function GetSelectedText () { if (window.getSelection)
{ var range = window.getSelection(); alert
(range.toString ()); } })();

jjb

Dimitris Papageorgiou

unread,
Jun 16, 2011, 11:50:42 PM6/16/11
to fir...@googlegroups.com
The issue was solved.

I followed your instructions.

Thanks
Reply all
Reply to author
Forward
0 new messages