PDA

View Full Version : Safari and getSelection not working. Can anyone identify the problem?



ooohyeah
Mar 5th, 2007, 02:59 AM
On Firefox 1.5 and 2.0 I've tested this code, and it looks fine. But on Safari (the newest version at least) the selection object seems to be broken:



var sel = window.getSelection();
alert(sel.toString());
var range = sel.getRangeAt(0);


The above code works in firefox, but not safari. in firefox I get the string contents in the alert popup box. in safari I get "Undefined" in the alert popup box.

Also in Safari the last line where it getrangeat basically crashes the javascript.

Anybody know why this happens?

Thanks

jkd
Mar 5th, 2007, 09:34 PM
Safari doesn't support getRangeAt(). The WebKit nightlies do, however. In the meantime, you will have to look at baseNode, baseOffset, extentNode, and extentOffset properties of the selection object to figure out where you are at.

ooohyeah
Mar 5th, 2007, 09:49 PM
Safari doesn't support getRangeAt(). The WebKit nightlies do, however. In the meantime, you will have to look at baseNode, baseOffset, extentNode, and extentOffset properties of the selection object to figure out where you are at.

how strange, it's part of the w3c dom standard, which safari says it supports?

What I really need is to get a range object such that I can continue to do my other functions. Is this possible to get a range object from the selection?

thanks

jkd
Mar 5th, 2007, 11:21 PM
how strange, it's part of the w3c dom standard,

No it's not. There is no standardized way of getting a user selection. As for getting a range, using the properties I described, you can. I'd post code I have written that does that (and works around the other bugs in Safari regarding selection), but I'm not sure I am allowed to. But, it can be done, if that's worth anything.

ooohyeah
Mar 5th, 2007, 11:31 PM
No it's not. There is no standardized way of getting a user selection. As for getting a range, using the properties I described, you can. I'd post code I have written that does that (and works around the other bugs in Safari regarding selection), but I'm not sure I am allowed to. But, it can be done, if that's worth anything.

jkd, if you could post the code, that would be awesome! i doubt it violates any rules here?

if you're unsure, please send me a private message with the code instead.

Thank you so much!!

jkd
Mar 6th, 2007, 01:12 AM
jkd, if you could post the code, that would be awesome! i doubt it violates any rules here?

if you're unsure, please send me a private message with the code instead.

Thank you so much!!

It's about NDA versus any CF thing. So no, I can't.

ooohyeah
Mar 6th, 2007, 05:06 AM
ok, I didn't know that.

Can you give me a little guidance on how I can go from a selection object to the range object? The range object I'm using works on firefox / opera, where I do cloneRange() and other methods. Does safari support those? Or am I going to have to handle safari ranges totally differently?

If I can get a range object that behaves the same as the one for w3c, can you give me some hints on how to get that range object using the methods you described above? I don't see them returning a "range" object, so I'm a bit confused.

Thanks again =)

jkd
Mar 6th, 2007, 06:43 AM
Just read up on the DOM2 Traversal spec. You'll want to use document.createRange(), range.setStart, and range.setEnd.