开发者

Detecting when inside an iframe and then hiding elements

I hope some of you better than me at coding can help.

I have a simple webpage (www.mypage.com). It has 5 links

Link 1 (www.mypage.com/link1.html) Link 2 (www.mypage.com/link2.html) Link 3 (www.mypage.com/link3.html) Link 4 (www.mypage.com/link4.html) Link 5 (www.mypage.com/link5.html)

Now from the main homepage, clicking on the link opens up a popup window using an iframe to display the page they clicked.

Now what I wa开发者_如何学Gont to do is that when people click the link via the mainpage and hence get a popup/iframe, that on that page eg (www.mypage.com/link1.html) I want to hide some elements. The elements are things link Menu and Banner.

Now if a person enters one of the links manually eg typing www.mypage.com/link1.html directly into their browser, then I want the Banner and Banner to show.

Is there anyway I can do this? Is there some javascript that can run that if it detects it's an iframe that it can do a display:none on the elements I want to hide?

Many thanks in advance.


This is how i would do it : in the link pages (www.mypage.com/link1.html) i would have a script to verify if the hash of the url has a certain value.If it does, then hide the banners;else show the banners normally. So when you open the page in an iframe, be sure to set the src to "www.mypage.com/link1.html#banner_off" and not to the simple "www.mypage.com/link1.html".
This way, when a user types in the browser the link address (without the hash value), your ads will be shown. here is an example of how the script in the link pages should look like:

function manageBanners(){
    if(document.location.hash == "banner_off")//hide banners
        {
            //code to hide banners here
            var banners = document.getElementsByClassName('banner');
            for(var i in banners)
                banners[i].style.display = 'none';
        }
    //else do not alter the banners visibility
}
window.onload = manageBanners;

Of course you can use in the same way the php-query like sintax : url?banner=false and check for the parameters in the url.
Hope this helps!


The best way I can think of to detect that a page is in an iFrame is to compare the URL of the page with the URL in the browser window. If they're different, it must be in a frame.

if (top.location != location) {
    // hide menu and banner
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜