programing

iframe 내부에서 iframe을 닫는 방법은?

telecom 2023. 2. 24. 13:15
반응형

iframe 내부에서 iframe을 닫는 방법은?

나는 게시물을 iframe에 로드하는 워드프레스 사이트를 가지고 있다.

동작하는 코드는 다음과 같습니다.

<a class="trick" rel="<?php the_permalink() ?>" href="<?php the_permalink() ?>"><?php the_title(); ?></a>

$(표준)ready(function){

    $.ajaxSetup({cache:false});
    $(".trick").click(function(){
        var post_link = $(this).attr("rel");
        $("#frame").css("display","block");
        $("#frame").attr("url", post_link);
        $("body").css("overflow","hidden");
    });

  });         </script>
<iframe id="frame" frameborder="no" allowtransparency="true" width="100%" height="100%" scrolling="no" src=""></iframe>

이제 iframe 내부에서 로드된 iframe을 닫으려면 어떻게 해야 합니까?

메인 페이지는 index.php(메인 워드프레스 루프), iframe의 내용은 머리글과 바닥글이 없는 single.php(싱글 포스트)입니다.

감사해요.


이게 내가 싱글로 산 노래야.php

<head>

<script src="http://code.jquery.com/jquery-latest.js"></script>
    <script>
    $(document).ready(function(){
        $("#close").click(function(){
            $('#frame', window.parent.document).remove();

             });

        });

    </script>


</head> 

<body>
<div id="container-single">
    <button id="close" >Close</button>



    <?php if (have_posts()) : while (have_posts()) : the_post(); ?>

        <article <?php post_class('single') ?> id="post-<?php the_ID(); ?>">

            <h1 class="entry-title"><?php the_title(); ?></h1>

            <div class="entry-content">

                <?php the_content(); ?>

                <?php wp_link_pages(array('before' => 'Pages: ', 'next_or_number' => 'number')); ?>

                <?php the_tags( 'Tags: ', ', ', ''); ?>

                <?php include (TEMPLATEPATH . '/_/inc/meta.php' ); ?>

            </div>


        </article>



    <?php endwhile; endif; ?>

    </div>

</body>

다음 코드를 실행합니다.single.php로딩되어 있습니다.iframe. 이렇게 하면iframe부모 사용window삭제 또는 숨깁니다.

//You can call hide() if you want to just hide it
$('#iframe', window.parent.document).remove();

사실 약간의 속임수를 알고 있어요.

상위 페이지에서 기능 만들기

var closeIFrame = function() {
     $('#iframeid').remove();
}

원하는 장소에서 콜을 종료하는 iframe 내부

parent.closeIFrame();

까다롭죠?

// Inside the iframe    
frameElement.remove();

Pinterest의 Pin It과 같은 북마클릿을 만들 때 이 문제를 발견했습니다.

교차 도메인에서 작동해야 합니다.

이 문제를 해결할 수 있는 유일한 방법은 iframe 내의 페이지와 GitHub의 다음 예시와 같은 상위 페이지 사이에 이벤트를 게시하는 것이었습니다.

https://gist.github.com/kn0ll/1020251

다른 스레드에 답변을 올렸습니다.https://stackoverflow.com/a/43030280/3958617

도움이 됐으면 좋겠다!

언급URL : https://stackoverflow.com/questions/9538886/how-to-close-iframe-from-inside-iframe

반응형