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>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?
  • ¥15 matlab(相关搜索:紧聚焦)
  • ¥15 基于51单片机的厨房煤气泄露检测报警系统设计
  • ¥15 Arduino无法同时连接多个hx711模块,如何解决?