469,647 Members | 1,805 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,647 developers. It's quick & easy.

document.getElementById comes out NULL help!

Hi,
I'm back again. Basically i'm trying to draw a box over an image which
is turning out to be a nightmare. The problem i'm getting at the moment
is that i'm creating a line with <div which works when it's not hidden
but I need to be able to make it hidden so I can use layers to show all
when it finished drawing to make it smoother. This is how some other
scripts are doing it that i've seen. So i've got it drawing a line and I
need getElementById to pick out the layer but it's only picking out NULL
with the one i've got enabled below. I've tried the others but they just
come out as errors. I need to get top2 which is the <div line to show
itself but I can't seem to reference it by getElementById. I use linux
mozilla so i need it working in mozilla as well as windows that's why
i'm using getElementById. My code is below any ideas anyone?

<html>
<head>
<script language="JavaScript" type="">
document.onmousedown = onmousedown;
var netscape = (document.layers) ? 1:0;
var goodIE = (document.all) ? 1:0;
var netscape6 = (document.getElementById && !document.all) ? 1:0;
var height = 100;
var width = 100;
var left = 50;
var top = 60;
var visible = false;

var name = 'secondtop';

str = '<div id="' + name +
'" style="position:absolute; overflow:none; left:' + left +
'px; top:' + top + 'px; width:' + width + 'px; height:' +
height +
'px;' + ' visibility:' + (visible ? 'visible;' : 'hidden;') +
'"><img name="left" src="/asdd/pixel.gif" border="1"></div>';

document.writeln(str);

var layer;
//layer = document.getElementById("' + name + '").style;
if(document.getElementById) {
//var show = document.getElementById;
//document.getElementById.(secondtop) = visible;
var name = 'top2'
//layer = eval('document.getElementById("' + name + '").style');
layer = document.getElementById("' + name + '");
//layer = eval('document.all.' + name + '.style');
//layer = document.layers[name];
alert("First" + layer);
}
else if(document.all) {
//return document.all[eN];
alert("Second");
}
else {
alert("Third");
//return null;
}

//layer.visibility = "visible";
layer.visibility = "show";

alert("hello");
function onmousedown(e)
{
document.writeln("hello");
}

</script>
</head>
<body bgcolor="FFFFFF">

<img border="1" src="iecadaptor.jpg">

<div id="top2" style="position:absolute; width:308px; height:1px;
z-index:1; left: 29px; top: 23px; overflow: visible; visibility: hidden;
background-color: black; layer-background-color: black; border: 1px none
#000000"></div>

</body>
</html>

Thanks

Gary
Jul 20 '05 #1
2 20698
On Tue, 02 Mar 2004 19:36:44 +0000, Gary Mayor <ga**@abertron.co.uk> wrote:
I'm back again. Basically i'm trying to draw a box over an image which
is turning out to be a nightmare. The problem i'm getting at the moment
is that i'm creating a line with <div which works when it's not hidden
but I need to be able to make it hidden so I can use layers to show all
when it finished drawing to make it smoother. This is how some other
scripts are doing it that i've seen. So i've got it drawing a line and I
need getElementById to pick out the layer but it's only picking out NULL
with the one i've got enabled below. I've tried the others but they just
come out as errors. I need to get top2 which is the <div line to show
itself but I can't seem to reference it by getElementById. I use linux
mozilla so i need it working in mozilla as well as windows that's why
i'm using getElementById. My code is below any ideas anyone?
The problem is that you're trying to access parts of a document that the
browser hasn't begun to parse yet. Place the code after the parts of the
document it is trying to access if the script must execute immediately.
You'll also probably have to close the output stream before accessing the
generated HTML. You might even need to use two separate SCRIPT elements;
it depends when the browser parses generated content.
<script language="JavaScript" type="">
From your previous thread:

Don't do that. The type attribute should be used to specify the scripting
language, not language (it is deprecated).

<script type="text/javascript">

is the correct way to write the tag (with optional src and defer
attributes).
var netscape = (document.layers) ? 1:0;
var goodIE = (document.all) ? 1:0;
var netscape6 = (document.getElementById && !document.all) ? 1:0;


Don't do that, either. There should not be any need to know the browser,
and detection by inference doesn't work anyway. Use feature detection, not
browser detection.

[snipped code]

Mike

--
Michael Winter
M.******@blueyonder.co.invalid (replace ".invalid" with ".uk" to reply)
Jul 20 '05 #2
Michael Winter wrote:
On Tue, 02 Mar 2004 19:36:44 +0000, Gary Mayor <ga**@abertron.co.uk> wrote:
I'm back again. Basically i'm trying to draw a box over an image which
is turning out to be a nightmare. The problem i'm getting at the
moment is that i'm creating a line with <div which works when it's not
hidden but I need to be able to make it hidden so I can use layers to
show all when it finished drawing to make it smoother. This is how
some other scripts are doing it that i've seen. So i've got it drawing
a line and I need getElementById to pick out the layer but it's only
picking out NULL with the one i've got enabled below. I've tried the
others but they just come out as errors. I need to get top2 which is
the <div line to show itself but I can't seem to reference it by
getElementById. I use linux mozilla so i need it working in mozilla as
well as windows that's why i'm using getElementById. My code is below
any ideas anyone?

The problem is that you're trying to access parts of a document that the
browser hasn't begun to parse yet. Place the code after the parts of the
document it is trying to access if the script must execute immediately.
You'll also probably have to close the output stream before accessing
the generated HTML. You might even need to use two separate SCRIPT
elements; it depends when the browser parses generated content.
<script language="JavaScript" type="">

From your previous thread:

Don't do that. The type attribute should be used to specify the
scripting language, not language (it is deprecated).

<script type="text/javascript">

is the correct way to write the tag (with optional src and defer
attributes).
var netscape = (document.layers) ? 1:0;
var goodIE = (document.all) ? 1:0;
var netscape6 = (document.getElementById && !document.all) ? 1:0;

Don't do that, either. There should not be any need to know the browser,
and detection by inference doesn't work anyway. Use feature detection,
not browser detection.

[snipped code]

Mike

Cheers Mike I used a function and called the show layer from a link and
it worked so it's the fact it hasn't executed the html yet as your
saying. I will change it so the javascript is after the html. I thought
it executed all javascript first but it doesn't. I've changed that
javascript call. Thanks a lot I can carry on now. I'm a perl programmer
having to learn javascript. It's great Cheers
Jul 20 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by David List | last post: by
3 posts views Thread by Catherine Lynn Smith | last post: by
12 posts views Thread by Kepler | last post: by
19 posts views Thread by vunet.us | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.