2016-06-30 2 views
0

자바 스크립트로 html 코드를 다운로드하고 싶습니다. 이 HTML 코드는 iframe에 표시 될 extern 웹 페이지입니다. 이 iframe에 HTML 코드가 표시되는 방법을 가져올 수 있습니까?자바 스크립트로 extern 웹 페이지 다운로드

그래서 여기 내 시도입니다 :

<body> 
    <iframe id="iframe_htmlcode1" name="iframe_htmlcode" src="http://www.w3schools.com"></iframe> 
<textarea id="TextArea1" name="S1"></textarea> 

    <script> 
     function getAndSetContent() { 

      var x = document.getElementById("iframe_htmlcode1"); 
      var y = (x.contentWindow || x.contentDocument); 

      document.getElementById("TextArea1").value = y.documentElement.innerHTML; 


     } 
</script> 
<input id="Button1" type="button" value="button" onclick="getAndSetContent()" /> 

그러나 y.documentElement.innerHTML는 아무 것도 반환하지 않습니다. 나는 또한 잘 작동하는 iframe에서 사용될 로컬 HTML 문서로 시도했다.

제안 사항?

감사

+0

를 발행합니다. 보안상의 이유로 도메인 간 예외가 발생합니다. 당신은 PHP에 요청을 보내야합니다, PHP는 그 HTML을 얻을 것이다 그리고 당신에게 다시 그것을 반환, 그 작품. –

+0

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Same_origin_policy_for_JavaScript – Teemu

답변

0

내가 당신의 코드는 프로토콜이 일치하기 때문에 로컬 파일로 작업한다고 가정 어쨌든 디버그는 모든 (당신은 몇 가지 기본적인 서버가 필요하고 다시 시도합니다)했다.

Uncaught SecurityError: Blocked a frame with origin "null" from accessing a frame with origin "http://www.w3schools.com". The frame requesting access has a protocol of "file", the frame being accessed has a protocol of "http". Protocols must match. 

당신이 고르 해결하기 위해 다음과 같은 것을 사용해야 서버에 코드를 일단은 불가능만큼 내가 아는 한

https://github.com/padolsey-archive/jquery.fn/tree/master/cross-domain-ajax

관련 문제