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

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 12-12-2010, 04:51 AM   PM User | #1
Toxhicide
New Coder

 
Join Date: Dec 2010
Posts: 18
Thanks: 4
Thanked 0 Times in 0 Posts
Toxhicide is an unknown quantity at this point
Get value from iframe, "is null or not an object".

Though I took out the source of the iframe, I get an error when I click the test button to get the value of msg_title. I am using IExplorer to debug it, as firefox doesnt show the error.

Code:
<script type='text/javascript'>
function test(){
page = window.location.href
document.getElementById('1').value = window.frames[2].document.getElementByName('msg_title').value
}
function apple(){
document.getElementById('3').innerHTML = '<iframe src="" name="4" id="4"></iframe>'
}
window.onLoad = test();
</script>
<div id='3' name='3'></div>
<input type='button' value='apple' name='apple' id='apple' onclick='apple()'>
<input type='button' value='test' name='test' id='test' onclick='test()'>
<input type='text' value='' name='1' id='1'>
<br>
<iframe src='' name='2' id='2'>
Toxhicide is offline   Reply With Quote
Old 12-12-2010, 06:26 AM   PM User | #2
Logic Ali
Regular Coder

 
Logic Ali's Avatar
 
Join Date: Sep 2010
Location: London
Posts: 960
Thanks: 0
Thanked 198 Times in 193 Posts
Logic Ali will become famous soon enoughLogic Ali will become famous soon enough
document.getElementByName is not a function

Firefox will show errors if you check the error console under 'Tools'.

Identifiers should not begin with a digit.

Quote:
Code:
window.onLoad = test();
Event handlers are all lower case and expect to be assigned a function reference:

Code:
window.onload = test;
I don't think that will fix all your problems.
Logic Ali is online now   Reply With Quote
Old 12-12-2010, 07:19 AM   PM User | #3
Toxhicide
New Coder

 
Join Date: Dec 2010
Posts: 18
Thanks: 4
Thanked 0 Times in 0 Posts
Toxhicide is an unknown quantity at this point
Yes, I put that code in there for who knows what reason. The error is with the window.frames['2'], though.
Toxhicide is offline   Reply With Quote
Old 12-12-2010, 07:32 AM   PM User | #4
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,036
Thanks: 197
Thanked 2,411 Times in 2,389 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
As Logic Ali says, an element name or id may not start with a digit.

It is your responsibility to die() if necessary….. - PHP Manual
Philip M is online now   Reply With Quote
Old 12-12-2010, 07:36 AM   PM User | #5
Kor
Red Devil Mod


 
Kor's Avatar
 
Join Date: Apr 2003
Location: Bucharest, ROMANIA
Posts: 8,478
Thanks: 58
Thanked 379 Times in 375 Posts
Kor has a spectacular aura aboutKor has a spectacular aura about
Let put it in other ways, as it looks like you understood nothing form what Logic Ali told you

1. There is no getElementByName() method in JavaScript. There is getElementsByName(), and it returns a collection, not a single element. Or you may use getElementById() instead. name and id are not the same thing.

2. name and id must start with a letter. Thus: id="2" and name="2" are HTML illegal.

3. window.frames[2] means the second frame/iframe (counted from top to bottom) window.frames['2'] means nothing, as I said, because name can not start with a digit.

4. window.frames[] is the reference of the iframe object, not the reference of the document loaded in the iframe.

So:
Code:
document.getElementById('valid_Iframe_Id').contentWindow.document.getElementsByName('msgtitle')[0].value
But it depends also on which kind of element has the name "msgtitle". If it is a form's element, it will work. If not, you'd better give that object an id, and use getElementById() instead.
__________________
KOR
Offshore programming
-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

Last edited by Kor; 12-12-2010 at 07:40 AM..
Kor is offline   Reply With Quote
Old 12-12-2010, 07:50 AM   PM User | #6
Toxhicide
New Coder

 
Join Date: Dec 2010
Posts: 18
Thanks: 4
Thanked 0 Times in 0 Posts
Toxhicide is an unknown quantity at this point
Yes, that seemed to fix it, but not I get "asdf is not defined.", and "apple is not a function.", added try{} because I got a permission error.

Code:
<script type='text/javascript'>
function asdf(){
page = window.location.href
try
{
document.getElementById('b').contentWindow.document.getElementsByName('msg_title')[0].value
}
}
function apple(){
document.getElementById('d').innerHTML = '<iframe src="" name="c" id="c"></iframe>'
}
</script>
<div id='d' name='d'></div>
<input type='button' value='apple' name='apple' id='apple' onclick='apple()'>
<input type='button' value='test' name='test' id='test' onclick='asdf()'>
<input type='text' value='' name='a' id='a'>
<br>
<iframe src='' name='b' id='b'>

Last edited by Toxhicide; 12-12-2010 at 07:56 AM..
Toxhicide is offline   Reply With Quote
Old 12-12-2010, 08:01 AM   PM User | #7
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,036
Thanks: 197
Thanked 2,411 Times in 2,389 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
You may not use the same name apple as the name of a function and an element (button) name or id.
Philip M is online now   Reply With Quote
Old 12-12-2010, 04:45 PM   PM User | #8
Toxhicide
New Coder

 
Join Date: Dec 2010
Posts: 18
Thanks: 4
Thanked 0 Times in 0 Posts
Toxhicide is an unknown quantity at this point
Issue solved.

Last edited by Toxhicide; 12-12-2010 at 09:27 PM..
Toxhicide is offline   Reply With Quote
Reply

Bookmarks

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 On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 06:34 PM.


Advertisement
Log in to turn off these ads.