document.getElementById comes out NULL help!

G

Gary Mayor

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
 
M

Michael Winter

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
 
G

Gary Mayor

Michael said:
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
 

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,774
Messages
2,569,596
Members
45,140
Latest member
SweetcalmCBDreview
Top