Using getSelection() with content in frames

Sunday 12/28/2008  –  Category: Uncategorized

An iPhoneSender user recently reported a bug with the Safari bookmarklet not correctly grabbing the selection and suspected it was because the selected content was in frames.

After some digging around, it turns out he was right--if your selection is in a frame, the normal window.getSelection() method will return nothing; you'll need to target the specific frame instead.  We don't know the frame's name so we can't target it with parent.frames["framename"], but what we can do is just loop over all the frames in the document until we find it.

(simplified and expanded):

Leave a Reply