-1

hello i have a problem with my code.. why it doesn't work..?? is there fault with my code?

function selectWord() {
        var select = window.getSelection();

        if (select.getBoundingClientRect) {        
            var rect = select.getBoundingClientRect ();
            x = rect.left;
            y = rect.top;
            w = rect.right - rect.left;
            h = rect.bottom - rect.top;

            alert (" Left: " + x + "\n Top: " + y + "\n Width: " + w + "\n Height: " + h);
        }
        else {
            alert ("Your browser does not support!");
        }
    }

thank you

user495688
  • 973
  • 2
  • 15
  • 25
  • 7
    What does "it doesn't work" mean? What output do you *expect*, and what do you *get*? – Andrzej Doyle Nov 12 '10 at 13:39
  • You got 4 implied globals (x, y, w, h), correct that. Also, why are you putting spaces between the function name and the call operator? Use `foo()` instead of `foo ()`. – Šime Vidas Nov 12 '10 at 13:54

2 Answers2

2

My guess is you are using this in a browser that does not support it. IE does not support getSelection and Fx 3.7 should be the first one to support the getBoundingClientRect

getBoundingClientRect problem with Firefox

Community
  • 1
  • 1
mplungjan
  • 169,008
  • 28
  • 173
  • 236
1

getBoundingClientRect is a DOM Node method, the result of the getSelection method is not a DOM Node.

There might be a way using the anchorNode, anchorOffset, focusNode and focusOffset properties of the Selection that is returned.

If you use firefox + firebug, you can do console.log(select) and inspect the properties that you have access to.

PottyBert
  • 1,902
  • 1
  • 13
  • 14