2014-03-03 2 views
1

그래서 클라이언트 측에서 일부 텍스트를 추출하여 처리를 위해 서버로 보내고 클라이언트 측에 iframe을 포함시킵니다. 그러나 오류가 발생하면 고객이 맞춤 오류 페이지를 생성하지 않고 iframe을 숨기거나 제거하려고합니다. 나는 이것을 달성하는 가장 좋은 방법이 무엇인지 궁금합니다. 여기가 고객에게주는거야 스크립트입니다 : 내가 시도 솔루션의 내부 서버 오류가 발생한 경우 iframe 숨기기

<script type="text/javascript"> 


$(document).ready(function(){ 

if($(".single-post").length===1) 
{ 
heading =$('.post h2').text(); 
heading =heading.trim(); 

link = $('.post h2 a').attr('href'); 

date = $('.post .meta .timr').text() 

if($('div .entry p').text()!="") 
para_full = $('div .entry p').text() 
total_length = para_full.length; 
if (total_length<3000) 
{ 
text_send = para_full; 
} 
else{ 

tsstart = (total_length/2) - 250; 
tsend = (total_length/2) + 250; 
text_send = para_full.substring(tsstart,tsend); 
} 

if($('div .entry img')[0].src !="" && $('div .entry img')[0].src != "undefined") 
image_url = $('div .entry img')[0].src 

if($('h3#comments').text()!="" && $('h3#comments').text()!="undefined") 
{ 
no_of_comments = $('h3#comments').text() 
l = no_of_comments.indexOf('Responses'); 
no_of_comments = no_of_comments.substring(0,l-1) 

} 
else 
{ 
    no_of_comments=0; 
} 


$(".client_site_iframe").attr("src","http://mydomain.com/query?"+"&searchtext="+text_send+"&link="+link+"&imagelink="+image_url+"&heading="+heading+"&date="+date+"&full_text="+para_full); 

} 

}) 

</script> 


<div class="client_site_container"> 
<iframe class="client_site_iframe" id="mysite_iframe" width="650px" height="205px" frameborder="no" scrolling="no"></iframe> 


</div> 

는 에러 핸들러 함수를 작성하는 것이었다는 다음과 같은 조건을 트리거됩니다

myHandler = function(error) { 
    document.getElementById('client_site_iframe').style.display = 'none'; 
} 

<script> 
if ($("#client_site_iframe html body").text().length === '') 
{myHandler();} 

</script> 

그러나 이것은 동일한 원본 정책으로 인해 작동하지 않았습니다. 사실, iframe 내부에서 생성 된 HTML 요소에는 클라이언트 쪽에서 jQuery를 사용하여 액세스 할 수 없습니다. 이 문제를 해결할 수 있는지 또는 AJAX 요청을 사용하여 권장하는 경우 잘 모르겠지만 클라이언트 측 스크립트에 매우 새로운 방법이 있는지 궁금합니다. 해결 방법 교차 출처 정책에 도움이 될 것입니다. 당신은 iframe의 내용에 액세스 할 수있는 경우

+0

tl; dr 가능한 경우 iframe 대신 https://api.jquery.com/jQuery.get/ 대신 ajax를 사용하는 것이 좋습니다. * 편집 : 교차 원산지 예외를 유발할 수 있으므로 귀하의 경우 불가능합니다 * – TastySpaceApple

+0

iframe의 콘텐츠가 다른 도메인에 있습니까? 그것에 약간의 빛을 비추었다. – Jai

+0

바로 ... 로저. 편집 됨. – TastySpaceApple

답변

1

, 당신은 할 수 :

  1. 은 iframe을 보여 iframe이 소스에서 parent.document.getElementById('client_site_iframe').style.display = 'block';를 사용하여 iframe이 내용을 성공적으로로드에 기본
  2. 으로 iframe을 숨기기 .
+2

iFrame 소스와 상위 소스가 같은 출처 인 경우에만 작동합니다. – ankits

+0

이것은 교차 출처 요청이기 때문에 동일한 출처 정책 예외를 여전히 트리거합니다. (iframe에서 부모로) – TastySpaceApple

+0

iframe의 상위 항목에 액세스하려고 할 때 프로토콜, 도메인 및 포트가 일치해야합니다. 이것들이 같으면 괜찮을 것입니다. – athms

관련 문제