document.getElementById comes out NULL help!

Discussion in 'Javascript' started by Gary Mayor, Mar 2, 2004.

  1. Gary Mayor

    Gary Mayor Guest

    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
     
    Gary Mayor, Mar 2, 2004
    #1
    1. Advertising

  2. On Tue, 02 Mar 2004 19:36:44 +0000, Gary Mayor <> 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
    d (replace ".invalid" with ".uk" to reply)
     
    Michael Winter, Mar 2, 2004
    #2
    1. Advertising

  3. Gary Mayor

    Gary Mayor Guest

    Michael Winter wrote:
    > On Tue, 02 Mar 2004 19:36:44 +0000, Gary Mayor <> 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
     
    Gary Mayor, Mar 3, 2004
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?QmlsbHk=?=

    document.getElementById("ReportViewerID") returns null

    =?Utf-8?B?QmlsbHk=?=, Jul 6, 2006, in forum: ASP .Net
    Replies:
    6
    Views:
    20,511
    Erik Funkenbusch
    Jul 7, 2006
  2. wk
    Replies:
    5
    Views:
    169
    Dietmar Meier
    Apr 22, 2005
  3. Martin
    Replies:
    3
    Views:
    149
  4. Replies:
    4
    Views:
    165
    Jonas Raoni
    Nov 21, 2006
  5. Replies:
    3
    Views:
    205
    Doug Gunnoe
    Jan 3, 2008
Loading...

Share This Page