해피정

2개 이상의 button 누르면 각 ifrme 으로 페이지 열기

· 2024-02-16 (금) 18:48:15 · 2609 · 3

샘플 : http://happyjung.com/demo/javascript/button_iframe.php

 

버튼 2개에 각각 iframe 으로 url 을 전송해서 페이지를 여는 방법입니다.

QA ( https://sir.kr/qa/525654 )에 질문을 했고

@마르스컴퍼니 님과 @배르만 님의 도움으로 문제를 해결하고 그 내용을 공유합니다.

 

<script src="//code.jquery.com/jquery-latest.min.js"></script>

<button type="button" id="bntPostYourAdd1">다음</button>
<button type="button" id="bntPostYourAdd2">카카오 캘린더</button>

<br><br>

<iframe id="forPostyouradd1" data-src="https://daum.net
    src="about:blank" width="500" height="300" style="background-color:coral">
</iframe>

<script>
    function postYourAdd1() {
        var iframe = $("#forPostyouradd1");
        iframe.attr("src", iframe.data("src")); 
    }
    $("#bntPostYourAdd1").on("click", postYourAdd1);
</script>


<iframe id="forPostyouradd2" data-src="https://map.kakao.com
    src="about:blank" width="500" height="300" style="background-color:aqua">
</iframe>

<script>
    function postYourAdd2() {
        var iframe = $("#forPostyouradd2");
        iframe.attr("src", iframe.data("src")); 
    }
    $("#bntPostYourAdd2").on("click", postYourAdd2);
</script>

 

 

참고자료

https://inpa.tistory.com/entry/open-iframe-on-button-click-Code

|

댓글 3개

2024-02-16 (금) 19:57:07

와우 수고하셨어요. 그런데 이리 하면 뒤로가기 버튼을 누르면 메인페이지 뒤로가기가 되는 것이 아니라 아이프레임 뒤로가기가 되어서 메인페이지는 아이프레임 페이지의 최초순간까지 뒤로가기가 안 되는 결점이...

그래서 div 의 innerHTML 을 바꾸는 것이 더 효과적일 수 있습니다.

 

아래 코드 한번 참고해 보세요.

 

[code]

<div id="buttonDiv">
    <button type="button" data-src="https://daum.net">다음</button>
    <button type="button" data-src="https://map.kakao.com">카카오 캘린더</button>
</div>
<div id="iframeDiv">
    <div style="background-color:coral;width:500px;height:300px"></div>
    <div style="background-color:aqua;width:500px;height:300px"></div>
</div>
<script>
buttons = document.querySelectorAll("#buttonDiv button"); 
for (i in buttons) {
    buttons[i].dataset.num = i;
    buttons[i].onclick = function() {
        document.querySelectorAll("#iframeDiv div")[this.dataset.num].innerHTML = "<iframe src='" + this.dataset.src + "' style='width:100%;height:100%;border:none'></iframe>";
    }
}
</script>

[/code]

@비타주리 
여윽시 비타주리님입니다요.

감사 합니다.

댓글을 작성하시려면 로그인이 필요합니다.

개발자팁

5,403건

개발과 관련된 유용한 정보를 공유하세요. 질문은 QA에서 해주시기 바랍니다.

+
분류 제목 글쓴이 날짜 조회
PHP 24-06-30 조회 1,240
jQuery 24-06-19 조회 1,270
PHP 24-05-27 조회 1,710
Mobile 24-05-21 조회 1,388
기타 24-05-11 조회 1,221
23-10-30 조회 2,133
기타 23-10-24 조회 2,659
23-12-19 조회 1,671
24-01-10 조회 1,692
24-01-10 조회 1,672
24-01-15 조회 1,444
24-02-09 조회 5,086
24-02-16 조회 2,506
24-02-16 조회 2,610
24-02-18 조회 3,501
24-02-22 조회 3,272
24-02-25 조회 4,360
24-03-09 조회 2,435
JavaScript 24-02-26 조회 1,444
OS 24-02-25 조회 1,483
PHP 24-02-23 조회 1,650
PHP 24-02-18 조회 1,535
PHP 24-02-17 조회 1,452
PHP 24-02-16 조회 1,577
PHP 24-02-05 조회 1,592
JavaScript 24-01-28 조회 1,439
PHP 24-01-28 조회 1,375
PHP 24-01-27 조회 1,342
PHP 24-01-27 조회 1,472
PHP 24-01-27 조회 1,437