Go Back   CodingForums.com > :: Client side development > JavaScript programming > Post a JavaScript

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 05-05-2012, 06:27 PM   PM User | #1
FutureWebDev
New Coder

 
Join Date: May 2012
Posts: 30
Thanks: 9
Thanked 0 Times in 0 Posts
FutureWebDev is an unknown quantity at this point
Practice Script - Iframe Interaction

When you assign an element of a window to a variable with getElementById() or getElementByName(), that variable is referring to an Element Object that is a property of the Window Object. This behavior is not true for frames, however; the value of the name or ID of a frame element refer to a Window Object as opposed to an Element Object.

This means that you can treat that frame in your code as if it were its own Window Object using the Name or ID that you have assigned to that frame. I provide an example below.

HTML
The id "fone" given to this <iframe> Element does not refer to an Element Object, it refers to a Window Object.
Code:
<p style="color:blue" onClick="chngFrameStyle();">Click Me</p>
<iframe id="fone" src="OreillyCh14v2Iframe.html" width="50%" height="400px" ></iframe>
JavaScript
fone is treated as a Window Object. This Window Object has an Element Object with the id "iframewrap" that will be assigned a backgroundColor value of #444.
Code:
function chngFrameStyle() {	
	fone.document.getElementById("iframewrap").style.backgroundColor = "#444";
}
Before Click

http://www.flickr.com/photos/77924248@N02/7145489961/


After Click

http://www.flickr.com/photos/77924248@N02/6999402854/
FutureWebDev is offline   Reply With Quote
Old 05-06-2012, 08:43 AM   PM User | #2
devnull69
Senior Coder

 
Join Date: Dec 2010
Posts: 2,261
Thanks: 10
Thanked 533 Times in 527 Posts
devnull69 will become famous soon enough
What is the browser restriction for this? It does not work for Firefox 12 at all!
Code:
fone is undefined
And with document.getElementById('fone').document I also get an exception
Code:
document.getElementById('fone').document is undefined
Usually you'll have to use .contentWindow or .contentDocument to get the respective window or document object from inside the iframe!
devnull69 is offline   Reply With Quote
Old 05-06-2012, 05:37 PM   PM User | #3
FutureWebDev
New Coder

 
Join Date: May 2012
Posts: 30
Thanks: 9
Thanked 0 Times in 0 Posts
FutureWebDev is an unknown quantity at this point
Quote:
Originally Posted by devnull69 View Post
What is the browser restriction for this? It does not work for Firefox 12 at all!
Code:
fone is undefined
And with document.getElementById('fone').document I also get an exception
Code:
document.getElementById('fone').document is undefined
Usually you'll have to use .contentWindow or .contentDocument to get the respective window or document object from inside the iframe!
Can you post the entire code so we can have a look see. I've only tested in FF 8.

Also the error "document is undefined". Isn't that pretty much impossible. Document is always defined right? If document is undefined then there is no way to define a variable to any of that documents properties. Which pretty much renders client-side scripting useless.

Last edited by FutureWebDev; 05-06-2012 at 05:40 PM..
FutureWebDev is offline   Reply With Quote
Old 05-07-2012, 10:32 AM   PM User | #4
devnull69
Senior Coder

 
Join Date: Dec 2010
Posts: 2,261
Thanks: 10
Thanked 533 Times in 527 Posts
devnull69 will become famous soon enough
It's not "document" which is undefined, it's rather the document property of your "fone" element.

Where did you define the fone element by the way? It's not mentioned in your code ...

I guess your code is highly depending on IE
devnull69 is offline   Reply With Quote
Old 05-07-2012, 12:52 PM   PM User | #5
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,102
Thanks: 197
Thanked 2,421 Times in 2,399 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
In IE9 I get "access is denied".
__________________

All the code given in this post has been tested and is intended to address the question asked.
Unless stated otherwise it is not just a demonstration.
Philip M is offline   Reply With Quote
Old 05-07-2012, 07:39 PM   PM User | #6
FutureWebDev
New Coder

 
Join Date: May 2012
Posts: 30
Thanks: 9
Thanked 0 Times in 0 Posts
FutureWebDev is an unknown quantity at this point
See below

Last edited by FutureWebDev; 05-07-2012 at 07:55 PM..
FutureWebDev is offline   Reply With Quote
Old 05-07-2012, 07:54 PM   PM User | #7
FutureWebDev
New Coder

 
Join Date: May 2012
Posts: 30
Thanks: 9
Thanked 0 Times in 0 Posts
FutureWebDev is an unknown quantity at this point
Ok I think I may need to take this post down. For some reason this isn't working now. Weird. I swear it worked. Let me play. If I can't figure it out by this evening I'll take down the post.


Sorry.

FWD
FutureWebDev is offline   Reply With Quote
Old 05-07-2012, 10:11 PM   PM User | #8
VIPStephan
The fat guy next door


 
VIPStephan's Avatar
 
Join Date: Jan 2006
Location: Halle (Saale), Germany
Posts: 7,699
Thanks: 5
Thanked 875 Times in 850 Posts
VIPStephan is a jewel in the roughVIPStephan is a jewel in the roughVIPStephan is a jewel in the rough
You can’t take down anything, you have to ask one of us.
__________________
Don’t click this link!
VIPStephan is offline   Reply With Quote
Old 05-07-2012, 11:31 PM   PM User | #9
FutureWebDev
New Coder

 
Join Date: May 2012
Posts: 30
Thanks: 9
Thanked 0 Times in 0 Posts
FutureWebDev is an unknown quantity at this point
Ok so the problem with this is that I need to take out any references to ID and keep only references to name.

You can reference with name but not ID.

So instead of:


Code:
<p style="color:blue" onClick="chngFrameStyle();">
			Click Me
		</p>
		<iframe id="fone" src="OreillyCh14v2Iframe.html" width="50%" height="400px" ></iframe>
Change id to name:
Code:
<p style="color:blue" onClick="chngFrameStyle();">
			Click Me
		</p>
		<iframe name="fone" src="OreillyCh14v2Iframe.html" width="50%" height="400px" ></iframe>
That should take care of it. Mods is there anyway I can edit my original post?
FutureWebDev is offline   Reply With Quote
Old 05-08-2012, 07:05 AM   PM User | #10
devnull69
Senior Coder

 
Join Date: Dec 2010
Posts: 2,261
Thanks: 10
Thanked 533 Times in 527 Posts
devnull69 will become famous soon enough
Works also in FF 12 ... I am surprised. I did not know that the chained notation with name attributes includes a document property for an iframe. You never stop learning ...
devnull69 is offline   Reply With Quote
Users who have thanked devnull69 for this post:
FutureWebDev (05-08-2012)
Old 05-08-2012, 03:14 PM   PM User | #11
FutureWebDev
New Coder

 
Join Date: May 2012
Posts: 30
Thanks: 9
Thanked 0 Times in 0 Posts
FutureWebDev is an unknown quantity at this point
Quote:
Originally Posted by devnull69 View Post
Works also in FF 12 ... I am surprised. I did not know that the chained notation with name attributes includes a document property for an iframe. You never stop learning ...
Yep. Thanks for testing again devnull69.
FutureWebDev is offline   Reply With Quote
Reply

Bookmarks

Tags
article, futurewebdev, iframe, javascript

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 04:11 PM.


Advertisement
Log in to turn off these ads.