Forum Moderators: open

Message Too Old, No Replies

How to detect if IFRAME loads in another page

         

IntegrityWebDev

5:58 pm on Jun 10, 2010 (gmt 0)

10+ Year Member



Hello all, I'm not much of a Javascript guy but I think this is probably the best place to find a solution for my need.

I have a WordPress blog. What I'm basically wanting to do is this:
1) If the blog is loaded in a browser run an HTML code bit.
2) If the blog is loaded via an IFRAME do not parse that same code.

What I have is that the WP blog shows the title in an H1 tag...I want that to show if its loaded as a free standing page. But if its loaded in an iframe, that iframe will already have a title and some other stuff, so I dont need to re-display the title from the blog.

Make sense?

Thanks!
Chris

IntegrityWebDev

7:01 pm on Jun 10, 2010 (gmt 0)

10+ Year Member



Just to let you know I figured out how to do this via PHP but one of the java gurus may want to explain it too.

What I did in PHP (psuedo code) was to call the URL within the IFRAME as example.com?showheader=no

Then in my WP header file added code that checked to see if the variable was site...if it was no, we dont show the HTML code in question, anything else and we do.

subexpression

12:29 am on Jun 11, 2010 (gmt 0)

10+ Year Member



IntegrityWebDev,

You can access the DOM elements of an iframe page and set any element to "display:none" with Javascript.

<script type="text/javascript">
function removeIframeHeadingTag(){
// REFERENCE THE IFRAME
var iframe = document.getElementById('wp-frame');
// REFERENCE THE HEADING DIV
var hd = iframe.contentDocument.getElementById('hd');
// REFERENCE THE H1 TAG
var h1 = hd.getElementsByTagName('h1');
// DISPLAY NONE
h1[0].style.display = 'none';
}
window.onload = function(){
removeIframeHeadingTag();
}
</script>

<iframe id="wp-frame" width="800" height="800" src="_url_to_your_wordpress_page_" />


This only works for iframes on your own domain.
You will have a "Permission Denied" error if you try to access a foreign page's DOM.

PHP works too, and you may like the control a bit better.
But as for me, I try as much as possible to let the user's computer do the work, instead of my server getting "nickeled and dimed" to death.
So, Javascript is my choice for those types of things.

IntegrityWebDev

12:45 pm on Jun 11, 2010 (gmt 0)

10+ Year Member



good point on easing the servers work. :-) Thanks!

Drag_Racer

12:08 am on Jun 12, 2010 (gmt 0)

10+ Year Member



The document inside the iframe has a frameElement property pointing to the iframe element in which it is contained. This element is supported by current browsers, including IE 5.5+ win, Safari, Firefox, and Opera (not sure about specific versions). So, if you can reference that property, then don't display the heading, else you do.