Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations sizbut on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

IFRAME javascript check

Status
Not open for further replies.

redletterrocko

Programmer
Aug 8, 2005
53
US
Alright, I'm currently implementing some pages inside IFRAMEs and have been trying to write some code that does not allow the page to be viewed outside of an IFRAME. This is what I've gotten so far:

Code:
<script>
<!--
if (top==self) {
document.write('<hr size=7 color=red><strong style="font-family:verdana;font-size:130%">');
document.write('ERROR: This page is intended to be inside an IFRAME!');
document.write('Please visit <a href="[URL unfurl="true"]http://eventuallyanyway.com">Eventually[/URL] Anyway</a> to see the actual page.');
document.write('</strong></hr>');
}
// -->
</script>
[code]

I'm not super experienced with javascript, but would like to implement another part of the script that makes sure it's part of the IFRAME, and then uses document.write to write the actual page.  Is that possible?  I would rather these pages not be viewable from an IFRAME, and lots of times, Google tends to screw things up and use those pages in its search, instead of the actual page.
 
I just need to know what the else statement should look like that would do document.write(actual web page) sort of thing. That link didn't really help me. I understand all of that. Sorry if I wasn't specific enough.
 
(I) would like to implement another part of the script that makes sure it's part of the IFRAME, and then uses document.write to write the actual page.
That will certainly prevent Google from directing visitors to your inside-the-iframe pages, as it won't see any content on them to index - and thus not rank them. You could get the same effect by adding [tt]<meta name="robots" content="noindex">[/tt] to the head of your framed pages, or using a robots.txt file. However, shutting out all incoming traffic from search engines like this seems like an extreme solution to your problem.
Google tends to screw things up and use those pages in its search, instead of the actual page.
Google's not screwed up here. Frames are a way of arranging several stand-alone pages onto a single screen. Google is an index of pages, and it ranks them according to their content. It's your design that's screwed up for relying on frames and expecting SEs to magically be able to link to a particular combination of pages.

What are you looking to achieve with iframes? If it's just a scrolling region of text within your page, you can do that with CSS. If you're looking to standardise navigation areas across the site, you can use SSI. There are better ways of doing it.

However, if you're stuck to/with frames, here's a Javascript solution that's a lot better than the one you've asked for: . It won't work for the allegedly-up-to-10% who surf with JS switched off, but there you go.

-- Chris Hunt
Webmaster & Tragedian
Extra Connections Ltd
 
It's just part of the layout that I'm using right now. I understand Google's not gonna be able to grab the pertinent information. However, there's a redirector that says "Look, this page needs to be accessed from Go there." Google will still pick up the content, because the code still contains what Google searches for. I think I'll just use php, 'cause I'm not really thrilled about javascript. I'm more comfortable with it anyway.
 
If the iframe knows what page it is supposed to be a part of, then instead of putting up an error message when the iframe finds it is the top frame just change the page to the one it should be: document.location.href = "the-top-page-url"

Tracy Dryden

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard. [dragon]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top