2009-12-09 9 views
2

id = "iView"및 designMode = on 인 iframe이 있습니다.IE에서 선택된 텍스트 가져 오기

내 코드 :

var iframeWindow = document.getElementById('iView').contentWindow.document;                        
var range = iframeWindow.getSelection().getRangeAt(0); 

오류 내가 얻을 :

Microsoft JScript runtime error: Object doesn't support this property or method

는 또한,
IE의 문서 객체에 대한 대해 getSelection 방법이 없습니다 how to get selected text from iframe with javascript?

답변

2

에서 답변을 시도 대신 선택 개체를 사용해야합니다.

var selText; 
var iframeWindow = document.getElementById('iView').contentWindow; 
if (iframeWindow.getSelection) 
    selText = iframeWindow.getSelection()+""; 
else if (iframeWindow.document.selection) 
    selText = iframeWindow.document.selection.createRange().text; 
+0

왜 iframeWindow.getSelection 또는 iframeWindow.document.selection을 확인해야합니까? – samuel

+0

기본적으로 브라우저 검사 인 기능이 있는지 확인하고 있습니다. 브라우저가 메서드를 지원하지 않으면 이러한 명령문은 false를 반환하고 스크립트는 실행되지 않습니다. 검사가 없으면 브라우저가 메서드를 지원하지 않으면 오류가 발생합니다. 또는 try/catch 문을 사용할 수도 있지만 일반적으로 기능 테스트의 기본 방법입니다. –

+1

@ samuel : 이것은 아주 좋은 답변이지만 언급 한 다른 질문에서 발견 된 것과 거의 같습니다 (http://stackoverflow.com/questions/1471759/how-to-get-selected-text-from-iframe --with-javascript), 어떻게 이것이 새로운 정보를 얻었습니까? –

관련 문제