I'm trying to use pure Javascript (no jQuery) to loop through each iframe on a page, then check to see if any of the iframes contain highlighted (selected) text. Then display an alert showing the highlighted text. The kicker is... the ID's of the iframes will not be known ahead of time. Hence the need to loop through each iframe. FYI - the iframes are all on the same domain, so no cross-domain issues here.
I think my code is pretty close to what I need, but no cigar so far.
On page load, I am doing this...
var iframes = document.getElementsByTagName('iframe');
for (var i = 0; i < iframes.length; i++) {
iframes[i].contentWindow.document.onmouseup = function() {
var iframeSelection = iframes[i].contentWindow.document.getSelection();
if (iframeSelection.toString().length > 0) { alert(iframeSelection); }
}
}
Also... for this particular project, it only needs to work in Chrome.
Thanks!