2016-06-16 7 views
1

에 에 대한 URL을 설정하는 가장 좋은 방법은 무엇입니까. 내 고객은 위치 또는 src 중 하나를 사용하여 프레임 URL을 설정/변경하고 싶습니다. 언젠가 위치 일, 언젠가 src 일 그래서 나는 무슨 일이 일어나는지 알게 될 것입니다.
나는 3 개의 html 파일을 만듭니다 : parent.html, left.html, right.html. 여기에 여기에 parent.html 내용내가 지금 <strong>프레임</strong> 및 <strong>프레임</strong> 함께 일하고 있어요 및 I 프레임의 URL을 변경하는 방법에 대해 너무 혼란 스러워요 <frameset>

<frameset cols="*,*"> 
    <frame id="left" src="left.html"> 
    <frame id="right" src="right.html"> 
</frameset> 

이 경우에 SRC

<head> <script> function LocationThis() { window.frames['innerleft'].location = "http://prntscr.com"; } function SrcThis() { window.frames['innerleft'].src = "http://prntscr.com"; } function LocationOther() { window.parent.frames['right'].location = "http://prntscr.com"; } function SrcOther() { window.parent.frames['right'].src = "http://prntscr.com"; } </script> </head> <body> <h1>Left</h1> <button type="button" onclick="LocationThis();">Location this</button> <button type="button" onclick="SrcThis();">Src this</button> <button type="button" onclick="LocationOther();">Location other</button> <button type="button" onclick="SrcOther();">Src other</button> <hr/> <iframe id="innerleft" src="" /> </body> 

내용
left.html 작품이다. 그럼 위치가 일 때 이 작동하지 않습니다. 좋아, 지금은 변경이

//window.parent.frames['right'].location = "http://prntscr.com"; 
window.parent.frames[1].location = "http://prntscr.com"; 

그런 위치 잘 작동과 같은 스크립트. 따라서 프레임 [ 'right']. src프레임 [1]. 위치입니다. 그러나 모든 경우가 아니며 때때로 프레임 [ 'right']. 위치도 작동합니다. 나는 프레임 ID 만 사용하여 이름을 제거하면 나는 위치를의

<frame name="right" src="right.html"> 

를 사용해야합니다. 아, IE 덕분에 src를 가져갈 수 없으므로 확인해야합니다.

try { 
    window.parent.frames["id"].src = {url}; 
} 
catch(er) { 
    window.parent.frames["id"].location = {url}; 
} 

이 모든 것들에 대한 깔끔한 해결책이 있습니까?

+0

http://stackoverflow.com/questions/1203068/why-should-i -not-use-html-frames –

+0

10 년 전의이 프로젝트.나는 IE5, 프레임 셋을 다루고있다 ... 그들에게 변경을 설득 할 수 없다. –

답변

1

크롬에서는 작동하지 않는다 (은 window.frames 개체에서 확인).하지만 프레임을 ID로 가져 오는 것이 HTML 요소에 대한 참조를 반환한다고 가정합니다. , 어떤이 src 특성이있다 - https://developer.mozilla.org/en-US/docs/Web/HTML/Element/frame

을 그리고 당신은 제대로 (인덱스로 얻기) 할 때, 당신은 location 특성이 그 프레임의 window 객체에 대한 참조를 얻을 - 그래서 https://developer.mozilla.org/en-US/docs/Web/API/Window

을, 내 생각 엔 window.frames['id'].src에서 작동합니다. 때문에 어떤 특질 브라우저 구현의 경우가 있지만, 실제로 옵션은 다음과 같습니다

  1. window.frames[index].location
  2. 참고 document.getElementById('FrameID').src
+0

당신은 file : ///로 실행할지도 모른다. ** window.frames [index] .location **은 3-4 프레임의 엉망이됩니다. ID로 가져 오기가 IE <8에서 작동하지 않는 것을 제외하고는 가장 좋은 선택이므로 try catch를 추가해야합니다 ... –