当直接链接指向子页面之一时,在其父页面中打开iframe页面 [英] Open iframe page in its parent when a direct link is to one of the child pages

查看:106
本文介绍了当直接链接指向子页面之一时,在其父页面中打开iframe页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有多个位于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.

示例: -此页面将是父页面并具有iframe -这将是第一页.php -然后会有第二个page.php,第三个页面php,第四个页面php等.

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.

如果有人链接到任何需要iframe的页面,而该页面需要使用iframe加载父页面并将url加载到该iframe中,则我希望这样做.因此,如果有人链接到第三页,它将加载父页面并在iframe中加载第三页.

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.

我尝试了以下脚本,这些脚本是我从网络上的帖子中找到的,但是它们仅加载框架中具有原始src的父页面.

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>

还有

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

我也尝试过:

<body onload="refreshFrame();">

然后我在在父框架(javascript)中打开iframe页面上看到了该帖子在这里,它只会显示链接到的页面,而不是iframe.

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.

iframe中的页面也是动态创建的,因此,如果与idk无关,请使用idk.

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

但是似乎没有什么可以打开我网站上iframe内部的页面,它仅加载具有原始src第一页面的父页面.因此,如果可以使用javascript或php,则可以使用idk.

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.

如果有人知道这种做法对搜索引擎优化有何影响,idk也不会在父页面上包含很多单词和标签,所以我也对此感到很好.

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.

谢谢

推荐答案

您是否尝试在iframe页面中使用base tag?像这样:

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

<base target="_parent" />

更新

好的,因此在您的情况下,您需要知道当前页面是在Brownser上还是通过iframe加载.如果它不在iframe上,请重定向到您调用iframe的另一页.

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.

首先,请检查当前页面是否位于带有js的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>

现在,我们知道是否无法通过iframe加载页面,我们可以在if状态中使用重定向:

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";

现在,您需要确保在重定向页面后,iframe src属性将与之前的URL相同.我假设您可以使用get参数int url来做到这一点?让我知道这部分是否有问题.

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

只需如上所述在URL中添加一个get参数即可.像这样:

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>

现在在您的index.php中(或包含iframe的页面中的任何内容):

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>

这篇关于当直接链接指向子页面之一时,在其父页面中打开iframe页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆