document.getElementById comes out NULL help!

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Gary Mayor

    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="JavaS cript" type="">
    document.onmous edown = onmousedown;
    var netscape = (document.layer s) ? 1:0;
    var goodIE = (document.all) ? 1:0;
    var netscape6 = (document.getEl ementById && !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.writel n(str);

    var layer;
    //layer = document.getEle mentById("' + name + '").style;
    if(document.get ElementById) {
    //var show = document.getEle mentById;
    //document.getEle mentById.(secon dtop) = visible;
    var name = 'top2'
    //layer = eval('document. getElementById( "' + name + '").style');
    layer = document.getEle mentById("' + 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.visibilit y = "visible";
    layer.visibilit y = "show";

    alert("hello");


    function onmousedown(e)
    {
    document.writel n("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
  • Michael Winter

    #2
    Re: document.getEle mentById comes out NULL help!

    On Tue, 02 Mar 2004 19:36:44 +0000, Gary Mayor <gary@abertron. co.uk> wrote:
    [color=blue]
    > 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?[/color]

    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.
    [color=blue]
    > <script language="JavaS cript" type="">[/color]

    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).
    [color=blue]
    > var netscape = (document.layer s) ? 1:0;
    > var goodIE = (document.all) ? 1:0;
    > var netscape6 = (document.getEl ementById && !document.all) ? 1:0;[/color]

    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.Winter@blueyo nder.co.invalid (replace ".invalid" with ".uk" to reply)

    Comment

    • Gary Mayor

      #3
      Re: document.getEle mentById comes out NULL help!

      Michael Winter wrote:[color=blue]
      > On Tue, 02 Mar 2004 19:36:44 +0000, Gary Mayor <gary@abertron. co.uk> wrote:
      >[color=green]
      >> 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?[/color]
      >
      >
      > 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.
      >[color=green]
      >> <script language="JavaS cript" type="">[/color]
      >
      >
      > 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).
      >[color=green]
      >> var netscape = (document.layer s) ? 1:0;
      >> var goodIE = (document.all) ? 1:0;
      >> var netscape6 = (document.getEl ementById && !document.all) ? 1:0;[/color]
      >
      >
      > 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
      >[/color]
      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

      Comment

      Working...