View Full Version : Quick javascript question
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).
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.
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)
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();
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!
That *is* accessing it through the DOM ;).
I meand doing the childnode thing and doing it manually through a tree.
vBulletin® v3.8.6, Copyright ©2000-2013, Jelsoft Enterprises Ltd.