Forum Moderators: open

Message Too Old, No Replies

How to detect frames?

if my page is being iframed

         

too much information

5:28 pm on Apr 5, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



This seems like it should be simple, but how do I tell if my page is being displayed through an iframe?

I am putting together a set of pages that will be displayed through iframe, but if they are not I need to add additional content. Is there a way to detect this or should I just pass a variable along with the call to the page in the iframe code?

Little_G

5:50 pm on Apr 5, 2006 (gmt 0)

10+ Year Member



Hi,

this will make a page jump out of a frame:


if ( top.location.href!= window.location.href ) {
top.location.href = window.location.href
}

The if statement will detect if the page is in a frame, you can change what it does if true to whatever.

Andrew

too much information

5:57 pm on Apr 5, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I knew it was simple, thanks Little_G

Little_G

6:00 pm on Apr 5, 2006 (gmt 0)

10+ Year Member



no problem

too much information

12:35 am on Apr 8, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



OK, another simple one...

How do pass the results to PHP within the same page? Or is it even possible?

Little_G

1:50 pm on Apr 8, 2006 (gmt 0)

10+ Year Member



Hi,

if I understand what your trying to do then this may help:


if(top.location.href!= window.location.href){
window.parent.location.href = "http://127.0.0.1/index.php?frame1=no";
}

The page detects if it is framed, if it is then it redirects the frameset page to a different url.

Andrew

too much information

4:55 am on Apr 9, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Well, here's what I'm trying to do...

I have a page on a site that I'm designing to be used in an iframe for my members to display through their sites. But they can also send people to my site directly to view the same page with a login in case they don't have their own site or don't know how to use HTML.

If they choose to send their customers to my site to view the page I want to display Adsense code, but if the page is shown in a frame I don't want the ads to appear.

The code you gave me works great in determining if the content I want to display should or should not be displayed, but the Adsense code loads a script using a "src=" method and I'm not sure how to add the adsense code within an IF statement.

Has anyone done this before?

kaled

9:57 am on Apr 9, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



A simpler way to break out of a frame is
if (top!=self) top.location.replace(location.href);

This also has the advantage that it doesn't break the Back button.

If the adsense code is stored on your server, you can modify it directly, however, if external to your site, you can use document.write() to insert the <script> tag necessary to load the adsense code.

Kaled.

too much information

3:38 pm on Apr 9, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I'm not really looking for a way to break out of a frame. I'm offering them a way to show the page without my ads by framing the page. When they frame the page I can display a link to my site for their competators to find and use my site.

I want to display the ads to their customers but only if they send their customers to my site to view their page. (It's a cart system that I'm redesigning)

Besides I believe that displaying Adsense on another site is against the TOS, which is what I'm trying to prevent.

Little_G

10:27 pm on Apr 9, 2006 (gmt 0)

10+ Year Member



Hi,

Maybe this will help you:


if(top.location.href == window.location.href){
document.write("<script type=\"text\/javascript\" src=\"http:\/\/pagead2.googlesyndication.com\/pagead\/show_ads.js\"><\/script>");
}

If the page is not framed then the Google script is loaded.

Andrew

too much information

6:43 am on Apr 10, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Well, that's what I am looking for, but unfortunately it doesn't do the trick.

I'm trying to use cookies to determine weather the page is framed earlier on, and then check the cookie to determine if the Adsense code should be displayed. So far so good, but setting cookies with a framed page is not that easy. I'm having trouble with the whole privacy policy thing.

I was hoping there was a way to pass a variable from Javascript directly to PHP in the same page, but I think cookies are the only way to do it.

kaled

11:01 am on Apr 10, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Javascript runs on the client, php runs on the server. The only way to pass information from javascript to php is via an url.

Try the following, its a little simpler than the version above.

<script type="text/javascript">
if (top==self) document.write('<script type="text/javascript" src="googleurl"><\/script>');
</script>

Kaled.

too much information

3:46 am on May 1, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Yea, I ended up passing variables from page to page, but then scrapped the whole idea. I just couldn't get the ads to look like they should belong so I dumped them.

No big deal, I have plenty of coverage in other areas of the site.

Thanks for the help though.