DHTML Problem

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Chris Leonard

    DHTML Problem

    Hi.

    I've copied some code from a book which will enable me to make a layer
    around a page, my aim is to do something a little more complex but this I
    thought would get me started.

    Anyway, I've run into a problem before I even start! The script works OK, it
    runs the image in from the left hand side quite nicely, great. Problem I
    have it always scrolls up to the top of the page, when the link I click
    maybe 2 or 3 screens below (does that make sence?). I thought if I was to
    add a name attribute to the link I could get the co-ordinates of this and
    the screen will not reposition.

    I tried this and I've come unstuck! I need to get the name of the link into
    the slide function ..... can some bright spark out there help me out please,
    I'm just no JavaScript expert!

    TIA.

    Chris


    <SCRIPT language="JavaS cript">
    var layername, xgoal, ygoal, xhop, yhop, delay=5;

    function checkDHTML() {
    if ((parseInt(navi gator.appVersio n)>=4) &&
    ((navigator.app Name !="Netscape" &&
    navigator.appVe rsion.indexOf(" X11") == 1) ||
    (navigator.appN ame != "Microsoft Internet Explorer" &&
    navigator.appVe rsion.indexOf(" Macintosh") == -1)))
    { return 1}
    else
    { document.locati on="nodhtml.htm "; return 0 }
    }

    function makeName(layerI D) {
    if (navigator.appN ame=="Netscape" )
    { refname = eval("document. " + layerID) }
    else
    { refname = eval("document. all." + layerID + ".style") }
    return refname
    }

    function slide() {
    if ((parseInt(laye rname.left) != xgoal) ||
    (parseInt(layer name.top) != ygoal))
    {
    layername.left = parseInt(layern ame.left) + xhop;
    layername.top = parseInt(layern ame.top) + yhop;
    window.setTimeo ut("slide()", delay)
    }
    }

    </SCRIPT>

    HTML Part.

    <DIV ID='picture0' STYLE='position : absolute; left: -500px; top: 10px;
    width: 300; z-index: 1'>
    <img src='pics/114974744.gif'>
    </DIV>
    <A name="link0" href="#" onClick="layern ame=makeName('p icture0'); yhop=0;
    ygoal=10; xhop=40; xgoal=100; slide()">
    Click me</A>


  • Lasse Reichstein Nielsen

    #2
    Re: DHTML Problem

    "Chris Leonard" <c.leonardNOSPA M@btinternet.co m> writes:
    [color=blue]
    > I've copied some code from a book which will enable me to make a layer
    > around a page, my aim is to do something a little more complex but this I
    > thought would get me started.[/color]

    Books are dangerous. A lot of them aren't worth the paper they are
    printed on. I fear you might have one of those.
    [color=blue]
    > Problem I have it always scrolls up to the top of the page, when the
    > link I click maybe 2 or 3 screens below (does that make sence?).[/color]

    It sounds like a classical problem :)

    You have a link of the form:
    <a href="#" onclick="some javascript">
    When you click it, the javascript is executed, and then you jump to the
    top of the document. If you look at the address line, you will notice that
    a "#" has been added to the address. That is because the browser follows
    the link to "#", which brings it back to the same page, only at the top.

    To avoid that the browser follows the link, the onclick handler must
    return false:
    <a href="" onclick="some javascript;retu rn false">
    (the "#" isn't needed, the empty string is a legal URL).


    Now some comments on the actual code. Muhahahahah! :)
    [color=blue]
    > <SCRIPT language="JavaS cript">[/color]

    <script type="text/javascript">

    The type attribute is mandatory, and the language attribute is
    deprecated, in HTML 4.
    [color=blue]
    > function checkDHTML() {
    > if ((parseInt(navi gator.appVersio n)>=4) &&
    > ((navigator.app Name !="Netscape" &&
    > navigator.appVe rsion.indexOf(" X11") == 1) ||[/color]
    ^ should be -1, right?
    Otherwise it won't match MS IE on Windows, which is probably not
    what you want.
    [color=blue]
    > (navigator.appN ame != "Microsoft Internet Explorer" &&
    > navigator.appVe rsion.indexOf(" Macintosh") == -1)))
    > { return 1}
    > else
    > { document.locati on="nodhtml.htm "; return 0 }[/color]

    window.location .href="nodhtml. htm";

    document.locati on isn't universally supported (i.e., it might fail
    for exactly those browsers that you don't think have DHTML either).
    No need to return after having dropped the entire page for a new one.
    [color=blue]
    > }[/color]

    No DHTML unless:

    -The version is 4+. Bad idea. A new browser that came out today would
    be version 1.0 (although it would probably have to claim to be version
    5+ to avoid problems with scripts like this). Only ever use the
    appVersion for anything if you know the browser type (e.g., appName).

    -It is not a non-Netscape on non-Unix(X11 actually) or non-Microsoft
    IE on a non-macintosh.

    Or more clearly: Not Netscape on Macintosh or IE on X11 (if the "1"
    should have been a "-1"). That is probably still not what you wanted,
    since it includes Netscape on Windows and any non-Netscape and non-IE
    browser.

    Luckily this function isn't used. :)
    [color=blue]
    > function makeName(layerI D) {
    > if (navigator.appN ame=="Netscape" )
    > { refname = eval("document. " + layerID) }[/color]

    refname = document[layerID];

    You definitly never need to use the eval function just to read the
    value of a property or variable.
    You probably never need to use the eval function at all.

    Not all versions Mozilla (if any) will have the "layer" as a property
    of the document object. I bet this code was created for Netscape 4,
    and doesn't know that Mozilla reports navigator.appNa me=="Netscape".

    In Mozilla (and derivatives like Netscape 7) and other modern browsers,
    you access the "layer" as
    refname = document.getEle mentById(layerI D);
    [color=blue]
    > else
    > { refname = eval("document. all." + layerID + ".style") }[/color]

    refname = document.all[layerID].style;
    [color=blue]
    > return refname
    > }
    >
    > function slide() {
    > if ((parseInt(laye rname.left) != xgoal) ||
    > (parseInt(layer name.top) != ygoal))[/color]

    The "left" and "top" properties are only present in IE and Netscape 4.
    Most modern browser have the properties "offsetLeft " and "offsetTop"
    which can only be read, not written. You use CSS to change position.

    Don't trust your books too much, they can be bad for you.
    At least realize that what they teach is very specific to IE and
    Netscape 4, and shouldn't be used in modern pages.

    Don't be discouraged, it takes some time to learn which books to trust
    and which not to.
    /L
    --
    Lasse Reichstein Nielsen - lrn@hotpop.com
    Art D'HTML: <URL:http://www.infimum.dk/HTML/randomArtSplit. html>
    'Faith without judgement merely degrades the spirit divine.'

    Comment

    • HikksNotAtHome

      #3
      Re: DHTML Problem

      In article <d6euwzs2.fsf@h otpop.com>, Lasse Reichstein Nielsen <lrn@hotpop.com >
      writes:
      [color=blue][color=green]
      >> function checkDHTML() {
      >> if ((parseInt(navi gator.appVersio n)>=4) &&
      >> ((navigator.app Name !="Netscape" &&
      >> navigator.appVe rsion.indexOf(" X11") == 1) ||[/color]
      > ^ should be -1, right?
      >Otherwise it won't match MS IE on Windows, which is probably not
      >what you want.
      >[color=green]
      >> (navigator.appN ame != "Microsoft Internet Explorer" &&
      >> navigator.appVe rsion.indexOf(" Macintosh") == -1)))
      >> { return 1}
      >> else
      >> { document.locati on="nodhtml.htm "; return 0 }[/color]
      >
      > window.location .href="nodhtml. htm";
      >
      >document.locat ion isn't universally supported (i.e., it might fail
      >for exactly those browsers that you don't think have DHTML either).
      >No need to return after having dropped the entire page for a new one.
      >[color=green]
      >> }[/color]
      >
      >No DHTML unless:
      >
      >-The version is 4+. Bad idea. A new browser that came out today would
      > be version 1.0 (although it would probably have to claim to be version
      > 5+ to avoid problems with scripts like this). Only ever use the
      > appVersion for anything if you know the browser type (e.g., appName).
      >
      >-It is not a non-Netscape on non-Unix(X11 actually) or non-Microsoft
      > IE on a non-macintosh.
      >
      > Or more clearly: Not Netscape on Macintosh or IE on X11 (if the "1"
      > should have been a "-1"). That is probably still not what you wanted,
      > since it includes Netscape on Windows and any non-Netscape and non-IE
      > browser.
      >
      >Luckily this function isn't used. :)[/color]

      It probably mentions an onload in the body tag (in the book) to execute the
      function to make sure the browser can handle the page.

      Why all the utterly useless browser detection at all?




      if(document.all && !document.getEl ementById) {
      document.getEle mentById = function(id) {
      return document.all[id];
      }
      }

      skips the need for testing for document.all at all and allows the use of
      getElementById, untested by me in anything but IE4.0 on a PC.
      Anybody that can test that in a browser that supports .all, but not
      ..getElementByI d other than IE4? I seem to recall Jim mentioning one that even
      supports .all and .layers at the same time.

      if (document.getEl ementById)
      {
      //lets use getElementById
      }
      else
      {
      if (document.layer s)
      {
      //lets use document.layers
      }
      else
      {
      alert('Your browser does not support the methods I need to make this page
      work')
      }
      }

      Now, if the browser supports what you want to try to do, it will do it.
      Otherwise, it will degrade. And all without knowing, or even caring, what
      browser is in use.

      --
      Randy
      All code posted is dependent upon the viewing browser
      supporting the methods called, and Javascript being enabled.

      Comment

      • Chris Leonard

        #4
        Re: DHTML Problem

        Thanks very much.

        Not really the answer I was looking for !!

        Oh well, perhaps you're right, I should have started off myself and not used
        the damn book, which to be fair is out dated but it's all I have and is a
        good reference .......... well, was a good reference in 2000 !!

        Chris


        Comment

        Working...