DocType impact on javascript

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

    DocType impact on javascript

    I'm exploring javascript as a newbie, and have noted that the DocType
    has a substantial impact on some of the things I'm trying to do. I
    guess certain features have been deprecated, and do not work when I mess
    with DocType settings.

    I'm playing with dynamic image positioning example off the web where an
    image has been positioned with CSS left and top positions gets moved
    with arrow keys.

    The example runs if I impose an HTML DocType of '//W3C//DTD HTML 3.2
    Final//EN' but it won't in HTML 4.01 or my desired "XHTML 1.0
    Transitional".

    Code info... You can try it out here:


    But simply, an image is instantiated on the HTML with:


    <div id="bird">
    <img src="bird.gif" width=16 height=16</div>

    ....and the CSS positions it with:

    <style type="text/css">
    #bird {position: absolute;
    top: 200px;
    left: 200px;
    }

    ....then in javascript,

    function move(deltax, deltay) {
    console.log("Mo ve Called and name is: " + thisBallPic.nam e);
    console.log(" Locn: " + thisBallPic.y);

    var x = new getObj("bird"); // func sets up the obj refs
    currentx += deltax;
    currenty += deltay;
    x.style.top = currenty;
    x.style.left = currentx;



    // position may change for next move
    if (currentx>swidt h) {
    currentx = 0; }
    if (currenty>sheig ht) {
    currenty = 0;}
    if ((currentx + iwidth)<0) {
    currentx = swidth; }
    if ((currenty+ihei ght)<0) {
    currenty = sheight;}
    }

    MY QUESTION: What element of this is not supported in the more recent
    HTML doc types and is there some substitution available to be XHTML 1.0
    friendly?

    Thanks in advance,
    Ross.

  • Martin Honnen

    #2
    Re: DocType impact on javascript

    RK wrote:
    ...and the CSS positions it with:
    >
    <style type="text/css">
    #bird {position: absolute;
    top: 200px;
    left: 200px;
    Here in your static CSS you correctly use a number (e.g. 200) and a unit
    (e.g. px) for your top and left properties.

    x.style.top = currenty;
    x.style.left = currentx;
    In your script code you need to do the same e.g.
    x.style.top = currenty + 'px';

    In quirks mode browsers might let you get away with assigning a number
    only but most doctypes trigger strict mode. See
    The MDN Web Docs site provides information about Open Web technologies including HTML, CSS, and APIs for both Web sites and progressive web apps.


    --

    Martin Honnen

    Comment

    Working...