getting nulls from window.frames[].document.getElementById

L

Logos

Hopefully you can shed some light on my issue!

I have a page with an iframe containing a table. The table rows and
contents are generated dynamically, without using innerHTML. (I have
a suspicion that this may be the problem - the dynamically created
nodes are not being put into the DOM)

I have some code that tries to pull a select box's value from an
iframe:
window.frames[0].document.getElementById("sboxLineQty|3").value;

This gives me an exception. Trying to retrieve the selectbox itself
using a similar statement returns null.

However, if I use this:
window.frames[0].document.getElementById("tblDetails").rows[1].cells
[0].firstChild.value

I get the selectbox's value, as I expect. However, this method is
less than desirable.

Thanks for any insight you might share on this!

Tyler Style
 
R

RobG

Hopefully you can shed some light on my issue!

I have a page with an iframe containing a table.  The table rows and
contents are generated dynamically, without using innerHTML.  (I have
a suspicion that this may be the problem - the dynamically created
nodes are not being put into the DOM)

I doubt that *not* using innerHTML is the problem.

I have some code that tries to pull a select box's value from an
iframe:
window.frames[0].document.getElementById("sboxLineQty|3").value;

It seems to me that you are using an illegal pipe (|) character in the
ID attribute:

"ID and NAME tokens must begin with a letter ([A-Za-z]) and may be
followed by any number of letters, digits ([0-9]), hyphens ("-"),
underscores ("_"), colons (":"), and periods (".")."

<URL: http://www.w3.org/TR/html401/types.html#type-name >


However, it doesn't cause any issues in Firefox or IE.
This gives me an exception.  Trying to retrieve the selectbox itself
using a similar statement returns null.

Which is why you get the exception: if getElementById can't find an
element with the specified ID, it returns null. The null object
doesn't have a value property (it doesn't have any properties at all).
However, if I use this:
window.frames[0].document.getElementById("tblDetails").rows[1].cells
[0].firstChild.value

I get the selectbox's value, as I expect.  However, this method is
less than desirable.

Which suggestes that the objects you created dynamically are indeed in
the DOM. Your problem is elsewhere, try posting a mimial example of
the issue, e.g.

[parent page]
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title> iFrame document </title>
</head>

<body>
<input type="button" value="Get value" onclick="
var x = window.frames[0]
.document.getElementById('foo|00').value;
alert(x);
">
<iframe src="frame0.html" width="400" height="500"
scrolling="auto" frameborder="1">
</iframe>
</body>
</html>

[iFrame frame0.html]

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title> iFrame content </title>
<link rel="stylesheet" type="text/css" href="mystyle.css">
<script type="text/javascript">

window.onload = function(){
var t = document.createElement('table');
var r = t.insertRow(-1);
var c = r.insertCell(-1);
var s = document.createElement('select');
c.appendChild(s);
s.options[0] = new Option('zero', 'zero');
s.options[1] = new Option('one', 'one');
s.options[1].defaultSelected = true;
s.id = 'foo|00';
document.getElementById('div0').appendChild(t);
}
</script>
</head>

<body>
<div id="div0"></div>
</body>
</html>
 
J

Jonas Raoni Soares Silva

I have some code that tries to pull a select box's value from an
iframe:
window.frames[0].document.getElementById("sboxLineQty|3").value;

Small advices:
- You should name the <iframe> to provide yourself a better access on
in @ frames["name"] @
- You've elected a quite strange id value by adding the pipe, no?
This gives me an exception.  Trying to retrieve the selectbox itself
using a similar statement returns null.

Can you send us the URL of where this happens? It will be reasonably
easier to find what's wrong.
 
L

Logos

It seems to me that you are using an illegal pipe (|) character in the
ID attribute:

"ID and NAME tokens must begin with a letter ([A-Za-z]) and may be
followed by any number of letters, digits ([0-9]), hyphens ("-"),
underscores ("_"), colons (":"), and periods (".")."

<URL:http://www.w3.org/TR/html401/types.html#type-name>

However, it doesn't cause any issues in Firefox or IE.

Didn't know that - I've always used pipes as token separators in
strings. Lucky for me that it's not strict, yet!
Which is why you get the exception: if getElementById can't find an
element with the specified ID, it returns null.  The null object
doesn't have a value property (it doesn't have any properties at all).

You're missing the point here - the element obviously exists or the
second method wouldn't work to retrieve a value. It SHOULDN'T be
returning null, OR throwing an exception. I obviously didn't make
that clear enough :p
However, if I use this:
window.frames[0].document.getElementById("tblDetails").rows[1].cells
[0].firstChild.value
I get the selectbox's value, as I expect.  However, this method is
less than desirable.

Which suggestes that the objects you created dynamically are indeed in
the DOM.  Your problem is elsewhere, try posting a mimial example of
the issue, e.g.

Oddly enough, a minimal example worked. I decided there must be an
error in my code somewhere as you suggested that I just couldn't see,
so I started retyping in the function. Aha! A spelling error! I
used sbox instead of sbx. AUGH! Fixed now. Sorry to have taken up
peoples' valuable time with such a tard thang...

Tyler Style
 
T

Thomas 'PointedEars' Lahn

Logos said:
RobG said:
It seems to me that you are using an illegal pipe (|) character in the
ID attribute:

"ID and NAME tokens must begin with a letter ([A-Za-z]) and may be
followed by any number of letters, digits ([0-9]), hyphens ("-"),
underscores ("_"), colons (":"), and periods (".")."

<URL:http://www.w3.org/TR/html401/types.html#type-name>

However, it doesn't cause any issues in Firefox or IE.

Didn't know that - I've always used pipes as token separators in
strings. Lucky for me that it's not strict, yet!

What are you talking about? This definition applies to *all* SGML-based
markup languages, such as HTML; it does not matter if you declare e.g.
HTML 4.01 Transitional or Strict.

<http://validator.w3.org/>

Note that NAME tokens are only in few instances required as values of
`name' attributes, though; most of those are of type CDATA instead (and
so may contain the `|' character). RTFM.
You're missing the point here - the element obviously exists or the
second method wouldn't work to retrieve a value.

It is impossible to say if you are correct here since you did not
provide enough context code.
It SHOULDN'T be returning null, OR throwing an exception.

Who knows.
I obviously didn't make that clear enough :p

You obviously didn't read the FAQ on how to post here.


PointedEars
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top