PDA

View Full Version : Quick javascript question


Muzer
11 May 2008, 17:16
Does anyone know how to refer to unnamed iframes in javascript, then get the selected text from them? Everything I've tried didn't work. This is to help improve my QuickQuote project. AFAIK in most cases there will only be one iframe on the page and if not it will be the last one (usually).

M3ntal
11 May 2008, 22:22
document.getElementsByTagName('iframe')[document.getElementsByTagName('iframe').length - 1]

quakerworm
12 May 2008, 05:00
get a firefox with dom inspector. it lets you see every object in the document, their properties, and everything you can do with them. for example, you'd be able to spot the getElementByTagName method of the document object right away.

Muzer
12 May 2008, 17:11
document.getElementsByTagName('iframe')[document.getElementsByTagName('iframe').length - 1]
Thanks .


EDIT: Would this work then?:
x = document.getElementsByTagName('iframe')[document.getElementsByTagName('iframe').length - 1]; insert = x.getSelection(); (insert being a variable)


EDIT2: Nope, I get a type error. message: Statement on line 1: Type mismatch (usually a non-object value used where an object is required)

M3ntal
13 May 2008, 13:01
You need to call getSelection() on the document object within the iframe rather than the iframe itself. I'm assuming this only needs to work in Opera 9, so i'll skip the cross-browser compatibility:


x = document.getElementsByTagName('iframe')[document.getElementsByTagName('iframe').length - 1];
insert = x.contentDocument.getSelection();

Muzer
13 May 2008, 17:52
Thank you so much. I'll try it now. I tried going to varios #javascript IRC channels and they were saying "oh, you have to access it through the dom" etc when I know that I wouldn't need to!

EDIT: Thank you so much, it works!

M3ntal
17 May 2008, 00:24
That *is* accessing it through the DOM ;).

Muzer
17 May 2008, 08:06
I meand doing the childnode thing and doing it manually through a tree.