...

View Full Version : Get value from iframe, "is null or not an object".



Toxhicide
12-12-2010, 05:51 AM
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.


<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'>

Logic Ali
12-12-2010, 07:26 AM
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.



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


window.onload = test;

I don't think that will fix all your problems.

Toxhicide
12-12-2010, 08:19 AM
Yes, I put that code in there for who knows what reason. The error is with the window.frames['2'], though.

Philip M
12-12-2010, 08:32 AM
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

Kor
12-12-2010, 08:36 AM
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:


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.

Toxhicide
12-12-2010, 08:50 AM
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.


<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'>

Philip M
12-12-2010, 09:01 AM
You may not use the same name apple as the name of a function and an element (button) name or id.

Toxhicide
12-12-2010, 05:45 PM
Issue solved.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum