2012年5月21日 星期一

Get selected text

Get highlighted text

var selection = window.getSelection().toString();

Get highlighted text in iframe
var iframe= document.getElementById('popupiframe');

if(iframe){
var idoc= iframe.contentDocument || iframe.contentWindow.document;
if(idoc){
var selectionInIframe = idoc.getSelection().toString();
}
}

Combined function. If no seleciton in iframe, get selection in document
var getUserSelection = function(){
Ti.API.info("getUserSelection@function.js");
var iframe= document.getElementById('popupiframe');
if(iframe){
var idoc= iframe.contentDocument || iframe.contentWindow.document;
if(idoc){
var selectionInIframe = idoc.getSelection().toString();
if(selectionInIframe && selectionInIframe!=''){
Ti.API.info("selection in iframe=#"+selectionInIframe+"#");
return selectionInIframe;
}
}
}
Ti.API.info("window="+window.getSelection().toString());
return window.getSelection().toString();
};

沒有留言:

張貼留言