ds19891231 2015-07-23 20:16
浏览 66
已采纳

当直接链接到其中一个子页面时,在其父项中打开iframe页面

Hi I have multiple pages that reside in an iframe and I would like them to open in the parent page when say a search engine indexes them or people directly link to them.

Example: -this page will be the parent page and have an iframe -this will be first page.php -then there will be second page.php, third page.php, fourth page.php, ect.

I would like it if someone linked to any of the pages that require the iframe to load the parent page with the iframe and load the url into that iframe. So if someone links to say third page it will load the parent page and load third page in the iframe.

I have tried the following scripts that I have found from posts across the web but they only load the parent page with the original src in the frame.

<SCRIPT LANGUAGE='javascript'>
    try { 
        if (top == self) {
            top.location.href = 'parent page.php'; 
        } 
    } catch(er) { } 
</SCRIPT>

And

<script type="text/javascript">
    if (window == top) {
        var url = 'window.location.replace("parent page.php?var1=' + window.location.href + '")';
        eval(url);
    }
</script>

I also tried:

<body onload="refreshFrame();">

Then I saw the post on Open iframe page in parent frame (javascript) here and it just brings up the page that was linked to and not the iframe.

The pages in the iframe are dymanically created as well so idk if that has anything to do with it or not.

But nothing seems to open the page inside of the iframe on my site it only loads the parent page with the original src first page in it. So idk if this is possible with either javascript or php.

Also idk if anyone knows how this practice effects seo there are not many words and no tags on the parent page so I was just curious about that too.

Thanks

  • 写回答

1条回答 默认 最新

  • duanfeigui6655 2015-07-23 20:24
    关注

    Did you try use the base tag in you iframe pages? Something like:

    <base target="_parent" />
    

    UPDATE

    OK, so in your scenario you need to know if the current page is loaded on brownser or via iframe. And if it's not on iframe, redirect to your another page where you calling the iframe.

    First thing first, check if the current page is inside a iframe with js:

    <script type="text/javascript">
        if ( self === top ) {
            //Its not an Iframe;
        }
    </script>
    

    Now that we know if the page isn't load via iframe we can use a redirect inside that if statament:

    window.top.location="http://www.yourdomain.com";
    

    Now you need to make sure after redirect the page, the iframe src attribute will be the same url it was before. I'm assuming you can do that with a get parameter int the url? Let me know if find trouble in that part.

    UPDATE2

    Just add a get parameter in the url as i mentioned above. Something like this:

    <script type="text/javascript">
        if ( self === top ) {
            //Its not an Iframe, needs redirect:
            var url = window.location;
            window.location="http://www.yourdomain.com/index.php?iframe="+url;
        }
    </script>
    

    Now in your index.php ( or whatever is the page that contain the iframe):

    <?php
        $iframe_url = (isset($_GET['iframe']) && !empty($_GET['iframe'])) ? $_GET['iframe'] : "initial-url-content";
    ?>
    
    <iframe src="<?=$iframe_url?>"></iframe>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!