Issue with a variable setting itself to the page?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • moltendorf
    New Member
    • Jul 2007
    • 65

    Issue with a variable setting itself to the page?

    Hey, I've got another issue, although my last one was never worked through... I'm pretty sure this is easy to solve. I've got a little javascript code and I would like to have it be able to fire an onmouseout without defining it explicitly within the HTML tags.

    I added in a little code to tell me what the variable itemId was set to when it couldn't locate the object in the page. It came out with the location of the page instead of the variable that I had passed to the function originally (and then again inside a function that calls itself).

    The focus is on lines 15 - 31.

    -----> Demo

    [HTML]<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dt d">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <script type="text/javascript">
    <!--
    function d2h(d) // Decimal to Hex
    {
    return d.toString(16);
    }
    function h2d(h) // Hex to Decimal
    {
    return parseInt(h,16);
    }
    function start(itemId)
    {
    var object = document.getEle mentById(itemId );
    if(!object)
    {
    document.write( "Error, itemId has been set to: "+itemId);
    }
    if(true) //object.onmouseo ver)
    {
    object.innerHTM L = "Shiney Link! (Mouse: Over)";
    setTimeout("sta rt("+itemId+") ; ", (1000));
    }
    else
    {
    object.innerHTM L = "Shiney Link! (Mouse: Off)";
    }
    }
    function restore(id)
    {
    // Nothing Yet
    }
    // -->
    </script>
    <title>Shiney Link!</title>
    </head>

    <body>
    We are testing the "Shiney Link!" JavaScript program below!<br />
    To test it, just simply move your mouse over the link.<br />
    In the future we will work on making images, and tables do the same.<br />
    <br />
    <a href="./" id="shiney" onmouseover="st art(this.id); ">Shiney Link! (Mouse: Off)</a>
    </body>
    </html>[/HTML]
  • Dasty
    Recognized Expert New Member
    • Nov 2007
    • 101

    #2
    I did not follow the purpose of your script. But you got error (typo?). in your code. you are sending id to your function as string value. But in setTimeout you forgot to add quotation marks:

    Code:
    setTimeout("start('"+itemId+"'); ", (1000));
    But your code will end up in infinity loop of calling start() for your item id (every 1 sec). But it's maybe because the code is not finished yet.

    Comment

    • acoder
      Recognized Expert MVP
      • Nov 2006
      • 16032

      #3
      What Dasty said, and...
      Originally posted by moltendorf
      I would like to have it be able to fire an onmouseout without defining it explicitly within the HTML tags.
      ...why not? Unless you mean inline, in which case you can use linkElem.onmous eout = ... or linkElement.add EventListener(" mouseout",...)/linkElement.att achEvent("onmus eout",...).

      Comment

      • moltendorf
        New Member
        • Jul 2007
        • 65

        #4
        Okay, that fixed the issue, but now, I'm having another issue - I intended to have it end when the user mouses off of it (notice how I commented out the object.onmouseo ver and used if(true) instead.) but it continues to be true even after I mouseoff.

        EDIT: I did change if(true) to if(object.onmou seover) on line 22.

        This would be solved if I did what acoder suggested, but I'm thrown by his response, if someone could explain in further detail about what he meant?
        Possibly use an actual example?

        Comment

        • acoder
          Recognized Expert MVP
          • Nov 2006
          • 16032

          #5
          Originally posted by moltendorf
          This would be solved if I did what acoder suggested, but I'm thrown by his response, if someone could explain in further detail about what he meant?
          Possibly use an actual example?
          What I suggested is similar to using an inline onmouseout without declaring it inline.

          For example,
          Code:
          object.onmouseout = someFunc;
          where someFunc() is a function.

          Comment

          Working...