PDA

View Full Version : Mozilla selectionStart / End



Dal
02-21-2009, 05:39 PM
I'm bouncing off the walls here trying to find the solution to this problem (my first problem in a long list ;))

I can successfully use selectionStart and selectionEnd when I am targetting a textarea object but for some reason firefox3 wont return these details for an IFrame?


var selectedTexty = document.getElementById('iView').contentWindow;
alert(selectedTexty);
alert(selectedTexty.getSelection());

var startPos = document.getElementById('iView').selectionStart;

alert(startPos);


Returns;
[object Window]

then...
asdasdasadasd asd asdas adas //my selection from within the editable iframe

then...
undefined //this would normally return a start position for the highlighted text when used in a textarea element but returns undefined when used with iframe?

Thanks
Dal

Master_script_maker
02-21-2009, 10:12 PM
try:
var startPos = document.getElementById('iView').contentWindow.selectionStart;
alert(startPos);

Dal
02-22-2009, 12:18 AM
Thanks for the reply but I already tried that. After I studied the mozilla wysiwyg example I relised that I needed to select a range object and use that as the reference to getting something called startOffset which returned the correct value. This seems to work until I make the text within the iframe formatted (for example bold)



var selectedTexty = document.getElementById('iView').contentWindow;
alert(selectedTexty);
alert(selectedTexty.getSelection());
var sel = selectedTexty.getSelection();

alert(selectedTexty.setSelectionRange);

var range = sel.getRangeAt(0);

sel.removeAllRanges();

var startPos = range.startOffset

alert(startPos);



Any ideas why the formatting would knock out the offset value?

Cheers
Dal

Edit: Actually upon further investigation it seems that it depends where the selection is and the offset comes from the inner tag if selected ie;
offset values: 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17
text: "this is normal text this is bold text"

I'm sure I'll figure it but it would be handy to have as much information about these details as possible as I've never used these objects before. Scanned the web looking for handy reference but they all seem to have some things that work and somethings that dont then there are the three schools (ff,ie,safari) which handle these all differently (which doesnt help the learning cycle at all!)

Cheers again
Dal :)

Master_script_maker
02-22-2009, 05:24 PM
here seams to be a very good website on ranges: http://www.quirksmode.org/dom/range_intro.html also this: http://the-stickman.com/web-development/javascript/finding-selection-cursor-position-in-a-textarea-in-internet-explorer/

Dal
02-22-2009, 06:01 PM
Cheers, I started hitting loads of problems after I figured out all the node controls so I think I'll put this project on the back burner for now. Seems that grabbing the selected text with mozilla doesnt grab the html code behind so when I make something like a word bold and then the sentence italic the bold goes missing.

I guess I'm put off by the fact that once I do get this thing build (wysiwyg editor), I then need to redo the whole thing for MSIE to work. I dont have that much time at the moment but I do intend to complete this project because I think I could learn alot from doing it and I'd also have something that I can embed into a cms system that belongs to me. For now I like the look of hoteditor although there are others I can use too.

Thanks Master_script_maker
Dal

Master_script_maker
02-22-2009, 07:54 PM
You're welcome. I hope you get to finish your project. If I find anything else I'll post it here.